2012-03-26 101 views
1

我有一個rspec /工廠女孩測試,我無法通過測試。Rspec工廠女孩問題

我在使用設計current_user調用當前登錄的用戶模型。

我可以加載了一個測試控制檯,輸入

u = Factory(:user) 
u.company 

,這將返回一個有效的公司,但由於某種原因在rspec的調用current_user.company將返回零。

任何想法?

控制器

class CompaniesController < ApplicationController 
    before_filter :authenticate_user! 

    def show 
    @company = current_user.company 
    end 
end 

模型

class User < ActiveRecord::Base 
    validates_uniqueness_of :email, :case_sensitive => false 

    has_one :company 
end 

Factory.define :company do |f| 
    f.name 'Test Company' 
end 

Factory.sequence(:email) do |n| 
    "person#{n}@example.com" 
end 

Factory.define :user do |f| 
    f.name 'Test User' 
    f.email {Factory.next :email} 
    f.password 'password' 
    f.company Factory(:company) 
end 

維護設備噸

describe CompaniesController do 
    before(:each) do 
    @user = Factory(:user) 
    sign_in @user 
    end 

    describe "GET show" do 
    before do 
     get :show 
    end 

    it "should find the users company" do 
     assigns(:company).should be_a(Company) 
    end 
    end 
end 

規格助手

RSpec.configure do |config|  
    config.before(:suite) do 
    DatabaseCleaner.strategy = :transaction 
    end 

    config.before(:each) do 
    DatabaseCleaner.start 
    end 

    config.after(:each) do 
    DatabaseCleaner.clean 
    end 

    config.infer_base_class_for_anonymous_controllers = false 
end 

測試結果

Failures: 

    1) CompaniesController GET show should find the users company 
    Failure/Error: assigns(:company).should be_a(Company) 
     expected nil to be a kind of Company(id: integer, name: string, user_id: integer, created_at: datetime, updated_at: datetime) 
     # ./spec/controllers/companies_controller_spec.rb:21:in `block (3 levels) in <top (required)>' 

EDIT

我已刪除了f.company =出廠(:COMPA ny)在工廠文件中。搞得我控制器規範這個

需要「spec_helper」

describe CompaniesController do  
    let(:current_user) { Factory(:user) } 

    before(:each) do  
    sign_in current_user 

    current_user.company = Factory(:company) 
    current_user.save 
    end 

    describe "GET show" do 
    before do 
     get :show 
    end 

    it "should find the users company" do 
     current_user.should respond_to(:company) 
     assigns(:company).should == current_user.company 
    end 
    end 
end 

回答

0

定義讓您的控制器rspec中的公司的對象。

describe CompaniesController do 
    describe "authorizations" do 
    before(:each) do 
    let(:company) { Factory :company } 
    let(:user_admin) { Factory(:user) } 
    end 

    it "should redirect" do 
    sign_in(user_admin) 
    get :show 
    end 

    it "should find the users company" do 
    assigns(:company).should be_a(company) 
    end 
end 
end 

你可以試試以上規格嗎?

+0

我不認爲你可以在前面的塊中使用let。 雖然我朝這個方向前進,但我會將我的代碼附加到我的主要帖子中 – Marklar 2012-03-28 19:44:53

1

我不知道,但我相信受讓人(:公司)檢查變量@company一個實例,這顯然不存在。嘗試將@company = @user.company放入before(:each)區塊或以其他方式對其進行測試,例如;

it "should find the users company" do 
    @user.should respond_to(:company) 
end 

我相信應該這樣做!

0

我認爲你失蹤的主要原因是在你的工廠建立了一個協會。從原來的例子開始:

Factory.define :user do |f| 
    f.name 'Test User' 
    f.email {Factory.next :email} 
    f.password 'password' 
    f.association :company, factory => :company 
end 

然後,當你創建一個用戶,它會創建一個公司,並在user.company_id用正確的ID填寫。

請參閱Factory Girl Getting Started文檔中的「關聯」。