2012-03-26 4 views
1

나는 테스트를 통과 할 수없는 rspec/공장 여자 테스트를 가지고있다.Rspec 공장 여자 문제

나는 current_user가 현재 로그인 한 사용자 모델을 호출하는 곳에 devise를 사용하고 있습니다.

나는

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 

테스 t

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)>' 

I는 f.company = 공장 (제거한

EDIT : 안돼요 ny)를 팩토리 파일에 추가하십시오. 분명히 존재하지 않는 @company 변수 인스턴스에 대한 검사 (기업) 그리고이

는 잘 모르겠어요하지만 양수인 생각 '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

정의 컨트롤러의 회사 개체를 입력하십시오.

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

을 필요로 내 컨트롤러 사양했다. 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 문서의 "연결"을 참조하십시오.