工厂女孩黄瓜步骤定义的问题

发布于 2024-12-02 14:25:50 字数 1747 浏览 0 评论 0原文

我正在使用工厂女孩提供的黄瓜步骤定义,但我无法在这里工作。

首先,这里是涉及的工厂:

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 :admin, factory: :user
end

这是我试图执行的步骤:

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

我收到此错误消息,并且完全不知道它来自哪里:

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

这是我的模型定义:

    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

顺便说一句,我将其与 spork 一起使用。

亲切的问候,

尼尔斯

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 :admin, 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

演多会厌 2024-12-09 14:25:51

哦,该死。知道了。我在过去的某个地方将 cache_classes 设置为 false,因为由于某种原因,正确的类重新加载不起作用。刚刚再次使其变为 true,现在它可以工作了。 :/

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. :/

九八野马 2024-12-09 14:25:50

这很可能是因为 Spork。

该错误是因为在某个时刻,User 常量正在重新加载,但 FactoryGirl 仍在引用旧常量。这是因为您可以像这样卸载常量:

Object.remove_const :User

请参阅此类中的行:

您可以通过以下方式查看发生此错误的位置断点或只是检查这两个地方周围的某个地方:

我的猜测是某些内容正在重新加载ActiveRecord 类,但不重新加载 FactoryGirl。解决这个问题的一种方法可能是重置 FactoryGirl 定义:

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

希望有帮助,Lance。

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:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文