Rspec的测试,呼叫控制器方法获得NoMethodError
所以我只是试图调用从用户控制器log_in方法在RSpec中作为Rspec的测试,呼叫控制器方法获得NoMethodError
it "should get the index page" do
@user = User.new({ :email => "[email protected]" })
log_in(@user)
get 'index'
response.should be_success
end
我得到的结果是一样
1) EmployeesController GET 'index' should get the index page
Failure/Error: log_in(user)
NoMethodError:
undefined method `log_in' for #<RSpec::Core::ExampleGroup::Nested_1:0x4ac0328>
# ./spec/controllers/employees_controller_spec.rb:11:in `user_log_in'
# ./spec/controllers/employees_controller_spec.rb:16:in `block (2 levels) in <top (required)>'
可有人帮帮我?由于
编辑2011年3月11日
这里是log_in方法是在UserController的
def log_in(user)
session[:current_user] = user.id
end
你为什么不存根LOGGED_IN?或者你的方法是什么。登录不是这个规范的目标,所以存根!这是一个简单的例子,我如何符合规范具有的before_filter控制器操作:
class MyController < ApplicationController
before_filter :logged_in?
def index
end
end
describe MyController do
describe "GET 'index'" do
context "when not logged in"
# you want to be sure that before_filter is executed
it "requires authentication" do
controller.expects :logged_in?
get 'index'
end
# you don't want to spec that it will redirect you to login_path
# because that spec belongs to #logged_in? method specs
end
context "when authenticated" do
before(:each) { controller.stubs :logged_in? }
it "renders :index template" do
get 'index'
should render_template(:index)
end
it "spec other things your action does when user is logged in"
end
end
end
我尝试使用存根,但是如果我在另一个控制器spec文件中,它似乎不起作用。在这种情况下,我在EmployeeController规范和调用UserController – Souloikj 2011-03-21 16:48:44
嗯......这很奇怪!为什么在EmployeeController规范中调用UserController?它不必对#logged_in做任何事情?对? – BurmajaM 2011-03-23 14:15:01
'@ BurmajaM'对于EmployeeController,有一个身份验证检查方式before_filter,我认为我需要登录才能通过这个before_filter – Souloikj 2011-03-29 21:12:25
你有没有机会使用Devise? – raidfive 2011-03-05 07:23:56
@raidfive不,我没有使用Devise – Souloikj 2011-03-07 17:59:37
方法'user_log_in'或'log_in'?你能包括这种方法吗? – raidfive 2011-03-07 19:05:41