我不知道我做错了什么,所以请帮帮我。
因为标题说所有在浏览器中都很好用。所以我猜这是我无法做到的规格。
我已经多次在我的助手,控制器和规范中重写了代码。
这是我现在的规格:
describe "visit as admin" do
before do
sign_in admin
visit organization_users_path # row 20 of users_controller_spec
end
it "should display right page" do
page.should have_selector('title', text: "Users")
end
end
当我运行它时,我得到:
1) Organization::UsersController visit as admin should display right page
Failure/Error: visit organization_users_path
NoMethodError:
undefined method `users' for nil:NilClass
# ./app/controllers/organization/users_controller.rb:5:in `index'
# ./spec/controllers/organization/users_controller_spec.rb:20:in `block (3 levels) in <top (required)>'
继承我的控制者和助手:
# helper
def current_organization
@current_organization ||= current_user.organization
end
def admin_user
unless current_user.admin?
redirect_to root_path
end
end
#controller
class Organization::UsersController < ApplicationController
before_filter :admin_user
def index
@users = current_organization.users
end
end
编辑1:来自rake routes
:
organization_users GET /organization/users(.:format) organization/users#index
答案 0 :(得分:1)
似乎在控制器的index
函数中:current_organization
是nil
。
请确保此助手返回有效对象:
def current_organization
@current_organization ||= current_user.organization
raise @current_organization.inspect # <--- use this to inspect this value and delete afterwards
end
答案 1 :(得分:0)
该错误告诉您current_organisation
为零,这似乎是合乎逻辑的,因为它是通过current_user加载的,在您的上下文中为nil。
您可以查看https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara以在测试中记录用户。
但是不要忘记在设置中记录作为组织的用户,以便current_organization帮助器不返回nil
编辑:我猜你正在使用设计登录,如果不是这样,请告诉我们,你用于认证的是什么