Tag: 工厂机器人

Rails FactoryGirl Factory具有可选的模型关联

按照rails 4模型关联左连接validationid我不得不更新我的工厂。 我正在尝试创建一个可以创建优惠券的条件,但是只有在执行时才分配作业ID,即: FactoryGirl.define do factory :coupon do code { rand(25**25) } percent_discount { rand(100**1) } start_at { Time.now } end_at { 30.day.from_now } trait :executed do |c| job c.executed_at { Time.now } end end end

Rails 4中的“Factory not registered”错误

我在Rails 4中使用Factory Girl获得了“Factory not registered”错误。这是我在spec / factories / user.rb中为一个简单的Devise用户定义的工厂: FactoryGirl.define do factory :user do email ‘test@example.com’ password ‘foobar’ password_confirmation ‘foobar’ end end 这是spec_helper.rb: ENV[“RAILS_ENV”] ||= ‘test’ require File.expand_path(“../../config/environment”, __FILE__) require ‘rspec/rails’ require ’email_spec’ require ‘rspec/autorun’ require ‘factory_girl’ Dir[Rails.root.join(“spec/support/**/*.rb”)].each { |f| require f } ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) RSpec.configure do |config| config.include(EmailSpec::Helpers) config.include(EmailSpec::Matchers) config.fixture_path = “#{::Rails.root}/spec/fixtures” config.use_transactional_fixtures […]

这两个FactoryGirl声明之间的实际区别是什么?

我发现这个错误让我花了太长时间才能找到。 我有这个: FactoryGirl.define do factory :global_list do list_id FactoryGirl.create(:user).liked_items_list.id end end 但只是包裹在一个块中: FactoryGirl.define do factory :global_list do list_id { FactoryGirl.create(:user).liked_items_list.id } end end 所以我知道在第二次调用中,阻塞导致它不会运行,直到实际调用如FactoryGirl.create(:global_list); 我认为这已经过去并通过FG转换为proc。 还有其他实际差异吗? 我希望有一种方法可以在详细模式下运行它们。 thx任何见解

重置FactoryGirl测试的“序列”

有谁知道如何重置FactoryGirl的序列方法? 我有一个工厂,创建一个任务列表,我想订购每次从1开始。 我使用’sequence’因为任务列表是一个关联的模型,所以每次我使用FactoryGirl.create时我都需要增加该命令,直到我调用一个reset。

与has_many关系的Factory Girl错误

我有以下工厂: Factory.define :email do |email| email.email {“infomcburney.cowan.com”} end Factory.define :lead do |lead| lead.emails {|emails| [emails.association(:email)]} end 以下是为以下类建模的 class Lead < ActiveRecord::Base has_many :emails end class Email “Lead”, :foreign_key => “lead_id” end 当我通过shoulda运行此测试时: should “capture emails” do lead = Factory.build(:lead) assert_equal(1, lead.emails.size) end 我收到以下错误: Factory :: AttributeDefinitionError:已定义的属性:电子邮件 我完全坚持这个,任何人都可以指出我正确的方向。 我正在使用factory_girl 1.3.2。

Rails / Devise – 我应该用devise和rspec测试什么?

许多程序员使用devise作为他们的身份validation解决方案,我想得到他们的建议: Devise已经过测试,但是我想知道是否有自己需要测试的东西(集成/单元/function测试?)与我的知识进行标准设计集成(我不熟悉shoulda和黄瓜,但我知道rspec和工厂女孩的一点) 谢谢你的建议!!

重新加载factory_girl工厂

我希望当我在工厂中进行更改时,我会在rails控制台中看到它们而不重新启动所有控制台。 我找到了一些线条并对它们进行了测试,对所发生的事情了解甚少, 文档对我来说并不清楚: FactoryGirl.reload 我也测试过: > FactoryGirl.factories.clear > FactoryGirl.find_definitions # also tested FactoryGirl.factories.find_definitions # but NoMethodError is raised => ActiveRecord::RecordNotFound: Couldn’t find Address with ID=277 Torphy Squares

获取工厂内的两个关联以共享另一个关联

我有这五个模型:卫报,学生,关系,关系类型和学校。 在他们之间,我有这些联想 class Guardian :destroy has_many :students, :through => :relationships end class Student :destroy has_many :guardians, :through => :relationships end class Relationship < ActiveRecord::Base belongs_to :student belongs_to :guardian belongs_to :relationship_type end class School :destroy has_many :students, :dependent => :destroy end class RelationshipType < ActiveRecord::Base has_many :relationships end 我想写一个定义关系的FactoryGirl。 每个关系都必须有一个监护人和一个学生。 这两个必须属于同一所学校。 监护人工厂与学校有联系,学生工厂也是如此。 我一直无法让他们在同一所学校建造。 我有以下代码: FactoryGirl.define do […]

railstutorial.org – 未定义的方法`工厂’

我正在尝试关注railstutorial.org,目前正在第7章开始使用工厂: http ://railstutorial.org/chapters/modeling-and-viewing-users-two#sec:tests_with_factories 我正在使用Rails 3.0.1和ruby-1.9.2-p0 我不能为我的生活让我的rspec测试通过,我得到的错误是 Failures: 1) UsersController GET ‘show’ should be successful Failure/Error: @user = Factory(:user) undefined method `Factory’ for # # ./spec/controllers/users_controller_spec.rb:9:in `block (3 levels) in ‘ 我的factories.rb看起来像这样: # By using the symbol ‘:user’, we get Factory Girl to simulate the User model. Factory.define :user do |user| user.name “Michael Hartl” user.email “mhartl@example.com” […]

工厂女孩多次has_many通过

我需要创建一些由多个工厂组成的工厂 这是我的模特 Topic has_many :plan_topics has_many :plans, :through => :plan_topics PlanTopic belongs_to :plan belongs_to :topic Plan has_many :subscriptions has_many :members, :through => :subscriptions has_many :plan_topics has_many :topics, :through => :plan_topics Subscription belongs_to :member belongs_to :plan Member has_many :subscriptions has_many :plans, :through => :subscriptions 这就是我所拥有的 Factory.define :topic do |topic| topic.name “Operations” end Factory.define :plan do |plan| […]