2015-08-28 76 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

所以它現在固定?很高興它修復:) –