开发者

Problem with the factory girl cucumber step definitions

开发者 https://www.devze.com 2023-04-02 06:39 出处:网络
I am using the cucumber step definitons provided by factory girl and I can not get something to work here.

I am using the cucumber step definitons provided by factory girl and I can not get something to work here.

First of all, here are the involved factories:

Factory.define :user do |u|
  u.name {|n| "User#{n}" }
  u.first_name {|n| "FirstName#{n}"}
  u.last_name {|n| "LastName#{n}"}
  u.password 'please'
  u.password_confirmation 'please'
end

Factory.define :lecture do |l|
  l.name {|n| "Lecture#{n}"}
  l.abbreviation {|n| "lec#{n}"}
  l.association :admi开发者_运维百科n, factory: :user
end

Here is the step I am trying to execute:

And the following Lecture exists:                                        
      | Name                                           | Abbreviation |
      | Informatik A - Algorithmen und Datenstrukturen | ainf         |

I am getting this error message and have absolutely NO idea where it comes from:

User(#42819220) expected, got User(#43753000) (ActiveRecord::AssociationTypeMismatch)
      features/lectures/ui.feature:11:in `And the following Lecture exists:'

And here are my model definitions:

    class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :rememberable, :trackable

      # Setup accessible (or protected) attributes for your model
  attr_accessible :name, :password, :password_confirmation, :remember_me, :first_name, :last_name

  validates_uniqueness_of :name

  has_many :administrated_lectures, class_name: "Lecture", foreign_key: "admin_id", dependent: :nullify
end

class Lecture < ActiveRecord::Base
  validates_uniqueness_of :name
  validates_uniqueness_of :abbreviation

  scope :ordered, order("name")

  belongs_to :admin, class_name: "User"
end

I am using this with spork btw.

Kind regards,

Nils


This is most likely because of Spork.

The error is because at some point the User constant is being reloaded, but FactoryGirl is still referencing the old constant. This is because you can unload constants like this:

Object.remove_const :User

See the line in this class:

  • https://github.com/thoughtbot/factory_girl/blob/master/lib/factory_girl/factory.rb#L27

You can see where this error occurs by breakpointing or just inspecting somewhere around these 2 places:

  • https://github.com/carlhuda/bundler/blob/1-0-stable/lib/bundler/runtime.rb#L68
  • https://github.com/rails/rails/blob/master/activesupport/lib/active_support/dependencies.rb#L519 (this class is the main place where you'll debug the problem)

My guess is that something is reloading ActiveRecord classes, but not reloading FactoryGirl. One way around this might be to reset the FactoryGirl definitions:

Spork.each_run do
  # this isn't the correct api, something like this though.
  FactoryGirl.definition_file_paths = [File.join(Rails.root, 'spec', 'factories')]
  FactoryGirl.find_definitions
end

Hope that helps, Lance.


Oh, damn. Got it. I set cache_classes to false somewhere in the past because proper class reloading did not work for some reason. Just made it true again, and now it works. :/

0

精彩评论

暂无评论...
验证码 换一张
取 消