2012-03-26 17 views
1

私はテストに合格できないrspec /工場の女の子のテストをしています。Rspec工場の女の子の問題

私はcurrent_userが現在ログインしているユーザーモデルを呼び出すdeviseを使用しています。

は私が

u = Factory(:user) 
u.company 

にテストコンソールとタイプをロードすることができますそして、これは有効な会社を返しますが、RSpecの呼び出しcurrent_user.companyで何らかの理由でnilを返しています。

アイデア?

コントローラ

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 

TESトン

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 =工場(削除されました:コンパを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

1

を必要をした私はわからないが、私は割り当て信じる:明らかに存在していないインスタンス変数@companyのためのチェック(会社を)。 before(:each)ブロックに@company = @user.companyを入れてみるか、別の方法でテストしてみてください。

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 

はその後、それが会社を作成し、適切なIDを持つuser.company_idに記入します。

Factory Girl Getting Started文書の「関連」を参照してください。

関連する問題