我正确使用工厂吗?

时间:2011-08-05 18:45:00

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

这是我目前的测试设置:

# spec/factories.rb
require 'factory_girl'

FactoryGirl.define do
  # Roles

  factory :user_role, :class => Role do
    name 'User'
  end

  # Users
  factory :user, :class => User do
    sequence(:email) {|n| "email#{n}@example.com" }
    password 'password'
    password_confirmation 'password'
    name 'Yuri Userington'
    roles { |a| [a.association(:user_role)] }
  end

  # Instruments
  factory :instrument, :class => Instrument do
    title "Doobie Doo Instrument Title"
    is_valid true
    association :user, :factory => :user
  end

  # Sequences
  sequence :email do
    "email#{n}@factory.com"
  end

end

# spec/controllers/instruments_controller_spec.rb
require 'spec_helper'

describe InstrumentsController do

  before (:each) do
    @instrument = FactoryGirl.create(:instrument)
    @attr = FactoryGirl.attributes_for(:instrument)
    @user = FactoryGirl.create(:user)
  end

  describe "GET index" do
    it "assigns all instruments as @instruments" do
      instrument = Instrument.new(@attr)
      instrument.user = @user 
      instrument.save!
      get :index
      assigns(:instruments).should eq([instrument])
    end 
  end

end

结果是,当我运行测试时,我的输出中出现以下错误:

Failures:

  1) InstrumentsController GET index assigns all instruments as @instruments
     Failure/Error: @instrument = FactoryGirl.create(:instrument)
     ActiveRecord::RecordNotFound:
       Couldn't find Role with id=2
     # ./app/models/user.rb:21:in `assign_role_after_sign_up'
     # ./spec/controllers/instruments_controller_spec.rb:24:in `block (2 levels) in <top (required)>'

基于此,似乎我的:用户工厂中的角色关联调用未被调用 - 我在这里做错了什么?我是以完全错误的方式使用它吗?

谢谢!!

2 个答案:

答案 0 :(得分:4)

这里有很多话要说。将您的代码与以下内容进行比较,以查看删除了多少行或单词。

FactoryGirl.define do
  # Sequences
  sequence :email do |n|
    "email#{n}@factory.com"
  end

  # Roles
  factory :user_role, :class => Role do
    name 'User'
  end

  # Users
  factory :user do
    email
    password 'password'
    password_confirmation 'password'
    name 'Yuri Userington'
    roles { |user| [Factory(:user_role)] } #many to many
   end

  # Instruments
  factory :instrument, :class => Instrument do
    title "Doobie Doo Instrument Title"
    is_valid true
    association :user #one-to-one or one-to-many
  end

end

在你的测试中:

describe InstrumentsController do

  before (:each) do
    @user = Factory(:user)
  end

  describe "GET index" do
    it "assigns all instruments as @instruments" do
      instrument = Factory(:instrument, :user => @user)
      get :index
      assigns(:instruments).should eq([instrument])
    end 
  end

end

此外:

  • 我个人更喜欢使用模拟和存根测试控制器

  • 我使用let代替实例变量而before_filter

答案 1 :(得分:2)

我有类似的问题,我使用回调来分配这样的角色:

Factory.define :user_with_admin_role, :parent => :user do |user|
  user.after_create {|instance| instance.roles << Factory(:admin_role) }
end

所以我认为你应该能够做类似的事情:

# Users
factory :user, :class => User do
  sequence(:email) {|n| "email#{n}@example.com" }
  password 'password'
  password_confirmation 'password'
  name 'Yuri Userington'
  after_create {|user| user.roles << Factory(:user_role) }
end

这是完全未经测试的,所以你可能需要调整一下。