1

我不知道我做错了什么,所以请帮帮我。

所以正如标题所说,在浏览器中一切都很好。所以我想这是我无法正确的规范。

我已经多次重写了我的助手、控制器和规范中的代码。

这是我现在的规格:

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
4

2 回答 2

1

似乎在index控制器的功能中:current_organizationis nil

请确保这个助手返回一个有效的对象:

def current_organization
  @current_organization ||= current_user.organization
  raise @current_organization.inspect # <--- use this to inspect this value and delete afterwards
end
于 2012-10-17T20:49:05.513 回答
0

该错误告诉您current_organisationis nil,这似乎是合乎逻辑的,因为它是通过 current_user 加载的,而 current_user 在您的上下文中是 nil。

您可以查看https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara以在测试中记录用户。

但是不要忘记在您的设置中记录一个作为组织的用户,这样 current_organization 助手不会返回 nil

编辑:我猜您正在使用设计进行登录,如果不是这种情况,请告诉我们您使用什么进行身份验证

于 2012-10-17T20:50:51.507 回答