Rspec工厂女孩问题

时间:2012-03-26 19:48:10

标签: ruby-on-rails ruby-on-rails-3 factory-bot rspec-rails

我有一个rspec /工厂女孩测试,我无法通过测试。

我正在使用设计,其中current_user调用当前登录的用户模型。

我可以加载测试控制台并输入

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

测试

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

Spec Helper

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

修改

我删除了工厂文件中的f.company = Factory(: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

3 个答案:

答案 0 :(得分:1)

我不确定,但我相信分配(:公司)检查实例变量@company,这显然不存在。尝试将@company = @user.company放入before(:each)块中,或以其他方式对其进行测试,例如;

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

我相信应该这样做!

答案 1 :(得分:0)

在控制器rspec中为公司定义Let对象。

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

你可以试试上面的规格吗?

答案 2 :(得分: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 doc。

中的“关联”