2012-03-26 106 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文档中的“关联”。