简体   繁体   English

Rspec工厂女孩问题

[英]Rspec factory girl issue

I have an rspec/factory girl test where I can't get a test to pass. 我有一个rspec / factory girl测试,但我无法通过测试。

I'm using devise where current_user calls the currently logged in User model. 我正在使用devise,其中current_user调用当前登录的用户模型。

I can load up a test console and type in 我可以加载测试控制台并输入

u = Factory(:user)
u.company

And this will return a valid company but for some reason in rspec calling current_user.company is returning nil. 这将返回一个有效的公司,但是由于某种原因在rspec中调用current_user.company返回nil。

Any ideas? 有任何想法吗?

Controller 调节器

class CompaniesController < ApplicationController
   before_filter :authenticate_user!

   def show
     @company = current_user.company
   end
end

Model 模型

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

  has_one :company
end

Factory

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

Test 测试

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

Test Result 测试结果

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 编辑

I have removed the f.company = Factory(:company) in the factories file. 我在工厂文件中删除了f.company = Factory(:company)。 And made my controller spec this 并使我的控​​制器规格

require 'spec_helper' 需要'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

I'm not sure but I believe assigns(:company) checks for an instance variable @company which obviously doesn't exist. 我不确定,但我相信@company (:company)检查实例变量@company ,该变量显然不存在。 Try putting @company = @user.company in your before(:each) block or test for it in another way, for example; 尝试将@company = @user.company放在您的before(:each)块中,或以其他方式对其进行测试,例如;

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

I believe that should do it! 相信应该做到的!

I think the main thing you were missing is setting up an association in your factory. 我认为您所缺少的主要事情是在工厂中建立协会 Starting from your original example: 从您的原始示例开始:

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

Then when you create a user, it will create a company and fill in user.company_id with the proper id. 然后,当您创建用户时,它将创建一个公司,并使用正确的ID填写user.company_id。

See "Associations" in the Factory Girl Getting Started doc. 请参见《 工厂女孩入门》文档中的“关联”。

Define Let object for company in your controller rspec. 在控制器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

Can you try with above spec ? 您可以尝试以上规格吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM