Rspec控制器測試,試圖為'processlogin'動作創建測試

[英]Rspec controller test, trying to create a test for a 'processlogin' action


My create user method in the users_controller.rb looks like:

我在user_controller.rb中的create user方法如下所示:

def process_login
   is_login_valid(params[:user][:user_name], params[:user][:password])

   if logged_in?
      redirect_to root_url
   else
      @user = User.new(params[:user][:user_name]
      redirect_to :action => 'login'
end

What I have currently:

我目前所擁有的:

describe UsersController do
  describe "Post 'process_login'"
    it "should be successful" do
       post 'process_login'

       response.should be_success
    end
  end

end

The methods is_login_valid and logged_in? are all included in the application_controller, and are methods from a ruby class I have in my /lib folder called LoginSystem.rb

方法是_login_valid和logged_in?都包含在application_controller中,並且是來自我/ lib文件夾中名為LoginSystem.rb的ruby類的方法

My test is failing since it isn't mocking things correctly, this is my first time doing this so hoping someone can help me out.

我的測試失敗,因為它沒有正確地嘲笑事情,這是我第一次這樣做,所以希望有人可以幫助我。

Error message:

UsersController POST 'process_login' should be successful
     Failure/Error: post 'process_login'
     NoMethodError:
       You have a nil object when you didn't expect it!
       You might have expected an instance of ActiveRecord::Base.
       The error occurred while evaluating nil.[]
     # ./app/controllers/users_controller.rb:11:in `process_login'
     # ./spec/controllers/users_controller_spec.rb:21

3 个解决方案

#1


2  

Aah, Thanks for the error message. I'm assuming that line 11 is is_login_valid(params[:user][:user_name], params[:user][:password]).

啊,謝謝你的錯誤信息。我假設第11行是is_login_valid(params [:user] [:user_name],params [:user] [:password])。

As you're not sending any params in your test post params[:user] is nil hence the nil.[] error (when the controller is looking for params[:user][:user_name]), you set params by passing them as a hash as the 2nd parameter to post in your test.

因為你沒有在你的測試帖中發送任何參數params [:user]是nil因此nil。[]錯誤(當控制器正在尋找params [:user] [:user_name])時,你通過傳遞它們來設置params作為在測試中發布的第二個參數的哈希。

#2


1  

I think you actually need

我想你確實需要

controller.stub(:logged_in?) { true }

Or if you want to test that the logged_in method is actually being called

或者,如果要測試是否實際調用了logged_in方法

controller.should_receive(:logged_in?).and_return(true)

The 2nd version will cause the test to fail unless the method logged_in? is called once and only once

第二個版本將導致測試失敗,除非方法logged_in?只召喚一次

You may also need the controller.stub(:is_login_valid} { true } as suggested by @jaydel if you're getting an error message about this method being missing as well

如果您收到有關此方法丟失的錯誤消息,您可能還需要@jaydel建議的controller.stub(:is_login_valid} {true}

#3


0  

I believe:

controller.stub(:is_login_valid} { true }

should get you where you want to go, if I understand correctly.

如果我理解正確,應該把你帶到你想去的地方。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/05/19/47d38c2a194e3bb17a585ebb201f7fef.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com