- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有人可以告诉我我的设置方式是否错误吗?
我有以下具有 has_many.through 关联的模型:
class Listing < ActiveRecord::Base
attr_accessible ...
has_many :listing_features
has_many :features, :through => :listing_features
validates_presence_of ...
...
end
class Feature < ActiveRecord::Base
attr_accessible ...
validates_presence_of ...
validates_uniqueness_of ...
has_many :listing_features
has_many :listings, :through => :listing_features
end
class ListingFeature < ActiveRecord::Base
attr_accessible :feature_id, :listing_id
belongs_to :feature
belongs_to :listing
end
我正在使用 Rails 3.1.rc4、FactoryGirl 2.0.2、factory_girl_rails 1.1.0 和 rspec。这是我对 :listing
的基本 rspec rspec 健全性检查工厂:
it "creates a valid listing from factory" do
Factory(:listing).should be_valid
end
这里是工厂(:列表)
FactoryGirl.define do
factory :listing do
headline 'headline'
home_desc 'this is the home description'
association :user, :factory => :user
association :layout, :factory => :layout
association :features, :factory => :feature
end
end
:listing_feature
和:feature
工厂的设置也类似。
如果association :features
该行被注释掉,然后我的所有测试都通过了。
当它是
association :features, :factory => :feature
错误信息是 undefined method 'each' for #<Feature>
我认为这对我来说是有意义的,因为 listing.features
返回一个数组。所以我把它改为
association :features, [:factory => :feature]
我现在得到的错误是 ArgumentError: Not registered: features
以这种方式生成工厂对象是不明智的,还是我错过了什么?非常感谢您的所有意见!
最佳答案
或者,您可以使用 block 并跳过 association
关键字。这使得无需保存到数据库即可构建对象(否则,即使您使用 build
函数而不是 create
,has_many 关联也会将您的记录保存到数据库中)。
FactoryGirl.define do
factory :listing_with_features, :parent => :listing do |listing|
features { build_list :feature, 3 }
end
end
关于rspec - 如何在 FactoryBot 中使用 has_many 关联设置工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6963298/
我想像这样使用 FactoryBot 随机返回特征: FactoryBot.define do factory :user do [:active, inactive].sample
在 5.0 FactoryBot 版本中,静态属性将被弃用,必须使用动态属性。所以: factory :product do name 'Some Product' end 需要变成: facto
我在测试中使用 factory_bot 创建对象,这里是我的工厂示例: factory :user do name "John" surname "Doe" trait :with_pho
我使用 factorybot有时在开发中创建记录。但是,它创建了一堆我没有预料到的额外数据。 我有两个工厂: FactoryBot.define do factory :user do f
我相信最好创建一个新问题...它遵循 my previous question我的模型产品有多种尺寸(嵌套属性) 我想创建工厂,但我不能让它工作...... 如果产品至少有一个尺寸( size_nam
我在 Rails 上使用 FactoryBot 和 Rspec。 我有一个 具体关键字 我扩展了 inittialize 的 ruby 类方法: def initialize(args) s
我正在学习如何在 Rails 中进行测试,并且正在为我的问题模型编写一个工厂: require 'factory_bot' FactoryBot.define do factory :questi
我有工厂 FactoryBot.define do factory :activity_fit_file do association :user, factory: :use
我有一个现有的工厂(订单),我正在尝试创建一个有效继承它的新工厂。它看起来像这样: factory :order_with_domain, :parent => :order do |o| o.o
是否有 FactoryBot 方法或某种方式来获取工厂的可用特征? 例如: FactoryBot.define do factory :address, class: Address do
给定两个模型,Alert 和 Zipcode,其中一个 Alert 必须有 1 个或多个 Zipcode: class Alert { :minimum => 1 } end class Zipco
当在 Gemfile 的开发和测试 block 中包含 factory_bot_rails gem 时,rails 将在生成模型时自动生成工厂。 有没有办法在生成模型后生成工厂? 注:FactoryB
在我的模型中,我有一个具有默认值的属性 foo。 我想编写一个与 foo 默认值相关的测试,但我的 FactoryBot 将其设置为 nil(并覆盖了默认值)。 如果我从工厂机器人中删除 foo 它会
我需要生成看起来像“ABC1”和“XYZ9”的标签序列——总是三个字母后跟一个数字。我希望标签的两个部分都按逻辑递增,并且始终保持准确的格式和长度。 我开始: sequence(:code) { |n
我有一个从休息 API 获取信息的对象。假设我从 API 调用 Posts,我可以指定要提取的字段。只是它的外观示例: Post.find!(id: 1, fields: [:id, :title,
我正在尝试在 heroku 中使用 gem FactoryBot.. 在本地一切正常,工厂已正确找到。 我正在尝试在 spec 文件夹之外使用工厂。 我在服务 invoice.rb 中使用它: cla
使用 Ruby gem 并尝试在 RSpec 中使用 FactoryBot。 我在 support/factory_bot.rb 中有这个: RSpec.configure do |config|
当我运行用户规范时,我收到 FactoryBot::DuplicateDefinitionError:Factory alreadyregistered:user 错误。我想创建多个具有不同名称和年龄
有人可以告诉我我的设置方式是否错误吗? 我有以下具有 has_many.through 关联的模型: class Listing :listing_features validates_pres
我遇到了有线问题,factoryBot 正在 Db 中创建记录,但是当 capybara 尝试访问它时,HTML 中没有记录。 我在提示时尝试使用“byebug”进行调试,当我说@topics =>
我是一名优秀的程序员,十分优秀!