2015-08-28 56 views
1

我想验证current_user的组织是否与他们试图查看的组织匹配。设计RSpec错误

这里是一个的失败此测试(@organization是在较早的方法被定义)的控制器的一部分:

if current_user.organization != @organization 
    redirect_to root_path, notice: "Not authorized to edit this organization" 
end 

这里的测试失败:

require 'rails_helper' 

RSpec.describe Admin::PagesController, :type => :controller do 
    describe 'GET #home' do 
    login_user 

    before do 
     @organization = FactoryGirl.create(:organization) 
    end 

    context "valid params" do 
     it "renders the home template and returns http 200" do 
     get :home, name: @organization.name 
     expect(response).to render_template("home") 
     expect(response.status).to eq(200) 
    end 
    end 
end 

这是我的工厂:

factory :user do 
    email { Faker::Internet.email } 
    organization_id 1 
    password "foobarfoobar" 
    password_confirmation { |u| u.password } 
end 

...这里是login_user被定义的地方:

module ControllerMacros 
    def login_user 
    @request.env["devise.mapping"] = Devise.mappings[:user] 
    user = FactoryGirl.create(:user) 
    sign_in user 
    end 
end 

堆栈跟踪:

1) Admin::PagesController GET #home valid params renders the home template and returns http 200 
Failure/Error: it "renders the home template and returns http 200" do 
    expecting <"home"> but rendering with <[]> 
# ./spec/controllers/admin/pages_controller_spec.rb:15:in `block (4 levels) in <top (required)>' 

但是:

[2] pry(#<RSpec::ExampleGroups::AdminPagesController::GETHome::ValidParams>)> subject.current_user.organization == @organization 
=> true 

不知道是怎么回事错在这里,似乎是相当标准的东西。有任何想法吗?

+0

最新错误?发布stacktrace请 –

+0

更新stacktrace – maclover7

+0

当您在您之前的块中创建组织时,您必须将该用户附加到该组织,如使用oraganization_id = @ organization.id创建用户 –

回答

1

原来的问题是我发送了错误的参数 - 应该发送@organization.subdomain而不是@organization.name。 :(

+0

所以它现在固定?很高兴它修复:) –