Tag: 工厂 机器人

使用子项存在validationFactory Girl创建父项和子项

有一个包含许多旅行的发票的项目。 新的故事以我的方式要求发票必须旅行。 我添加了一个validationvalidates :trips, presence: true trip validates :trips, presence: true但是现在它已经炸毁了我的一些测试,因为FactoryGirl在创建相关行程之前试图保存发票。 FactoryGirl.define do factory :invoice do sequence(:invoice_id) { SecureRandom.uuid} merchant amount 100.00 item_count 1 paid false currency “GBP” invoice_type “pre-flight” service_rendered false cancelled false after(:create) { |object| create(:trip, invoice_id: object.invoice_id)} end end 我该怎么做才能创建这些对象。 最好是在工厂级别,因为有许多测试利用这种行为(并且目前由于它而失败)。 这似乎是测试级别的一个很好的解决方案。 更新仍然努力让我的测试现在变绿。 42使用以下代码进行测试时出错。 validation失败:旅行不能为空 我在FactoryGirl代码中的当前更新行 before(:create) { |object| object << build(:trip, […]

FactoryGirl工厂带有Trait(s),它返回带有弦乐键的Hash

我正在尝试使用FactoryGirl来构建一个返回如下内容的Hash : => {“3″=>”1”, “6”=>”Word”} 我已经接近但不是100%那里…… 我试过的第一个工厂定义看起来像这样: factory :faqtory, class: Hash do |f| f.ignore do fake_word Faker::Lorem.word end f.sequence(1.to_s) { |n| n } f.send(2.to_s, fake_word.to_s.capitalize) initialize_with { attributes.stringify_keys } end 不幸的是,这不起作用: 1.9.3p448 :001 > FactoryGirl.build :faqtory ArgumentError: Trait not registered: fake_word 在那之后不起作用我假设对fake_word的调用需要在一个块中,但这没有任何区别。 有什么建议?

FactoryGirl覆盖关联对象的属性

这可能很简单,但我无法在任何地方找到一个例子。 我有两个工厂: FactoryGirl.define do factory :profile do user title “director” bio “I am very good at things” linked_in “http://my.linkedin.profile.com” website “www.mysite.com” city “London” end end FactoryGirl.define do factory :user do |u| u.first_name {Faker::Name.first_name} u.last_name {Faker::Name.last_name} company ‘National Stock Exchange’ u.email {Faker::Internet.email} end end 我想要做的是在创建配置文件时覆盖一些用户属性: p = FactoryGirl.create(:profile, user: {email: “test@test.com”}) 或类似的东西,但我不能正确的语法。 错误: ActiveRecord::AssociationTypeMismatch: User(#70239688060520) expected, […]

FactoryGirl:工厂未注册:user(ArgumentError)

使用FactoryGirl以正确的顺序获取所有鸭子很麻烦。 设置一个简约的rails应用程序(3.0.11),factory_girl_rails(1.4.0),factory_girl(2.3.2)和cucumber-rails(1.2.1)以及ruby-1.8.7-p352。 黄瓜测试是: Feature: a Scenario: test factory-girl Given the following user exists: | name | email | | Brandon | brandon@example.com | 结果如下: cucumber Using the default profile… “Adam Advertiser” “a@b.com” # Feature: a Scenario: test factory-girl # features/users.feature:2 Given the following user exists: # factory_girl-2.3.2/lib/factory_girl/step_definitions.rb:100 | name | email | | Brandon | […]

has_many同时尊重factory_girl中的构建策略

情况 # Models class User < ActiveRecord::Base has_many :items end class Items :user) do |u| u.items {|items| [items.association(:item), items.association(:item)]} end Factory.define(:item) do |i| i.color “red” end Factory.define(:item_with_user, :parent => :user) do |i| i.association(:user) end 问题 如果你运行@user = Factory(:user_with_items)那么@user.items包含这两个项目。 问题是这些项目与数据库中的用户没有关联。 如果你重新加载关联@user.items(true)那么你会得到一个空数组。 我知道你可以手动构建它们或者自己创建辅助方法来构建对象图,但我想避免这种情况。 题 所以,我的问题是如何在尊重构建策略的同时在factory_girl中建立一个has_many关系?