我必须为我的一个功能列表页面编写集成测试用例,并且该功能索引方法具有如下代码
def index
@food_categories = current_user.food_categories
end
现在,当我尝试为此编写测试用例时,它会抛出错误
'undefined method features for nil class' because it can not get the current user
现在我要做的是下面
我在每个语句之前编写了登录过程,然后编写了功能列表页面的测试用例
你能告诉我如何获得current_user
?
仅供参考,我已经使用了 devise gem 并使用 Rspec 进行集成测试用例
Here is 我的规格文件 https://gist.github.com/2319172这是我的food_categories_spec.rb https://gist.github.com/2319177
Update:您混淆了功能测试和集成测试。集成测试不使用get
,因为没有要测试的控制器操作,所以您必须使用visit
(一些网址)。然后您必须检查页面的内容,而不是响应代码(后者用于功能测试)。它可能看起来像:
visit '/food_categories'
page.should have_content 'Eggs'
page.should have_content 'Fats and oils'
如果您需要功能性的测试一下,这是一个例子:
# spec/controllers/your_controller_spec.rb
describe YourController do
before do
@user = FactoryGirl.create(:user)
sign_in @user
end
describe "GET index" do
before do
get :index
end
it "is successful" do
response.should be_success
end
it "assings user features" do
assigns(:features).should == @user.features
end
end
end
# spec/spec_helper.rb
RSpec.configure do |config|
#...
config.include Devise::TestHelpers, :type => :controller
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)