Tag: factory bot

如何使用attr_accessible创建工厂?

如何处理工厂和attr_accessible ? 我的例子: # model class SomeModel attr_accessible :name, full_name, other_name end #spec require ‘spec_helper’ describe “test” do it do create(:some_model, name: “test name”, user: User.first) #factory end end #error ruby(17122,0x12a055000) malloc: *** error for object 0x8: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug 我认为错误是因为user_id不在attr_accessible属性中。

没有rails的RSpec没有加载spec / support中的文件

在文件spec / support / factory_girl.rb中我有 fail “At least this file is being required” RSpec.configure do |config| config.include FactoryGirl::Syntax::Methods end 我有一些规范代码 require ‘pmc_article’ require ‘pmc_article_parser’ require ‘factory_girl’ require_relative ‘./factories/pmc_article_factory’ RSpec.describe PMCArticle do let(:pmc_article) do build(:pmc_article) end it ‘parses pmid’ do expect(pmc_article.article_id_pmid).to eq ‘123456’ end end 但是当我运行bundle exec rspec我得到了 1) PMCArticle parses pmid Failure/Error: build(:pmc_article) NoMethodError: undefined […]

为什么我得到FactoryGirl错误的参数数量错误?

FactoryGirl在非rails应用程序中有奇怪的行为。 错误的参数数量错误…不知道为什么。 gideon@thefonso ~/Sites/testing (master)$ rspec spec /Users/gideon/.rvm/gems/ruby-1.9.3-p194/gems/factory_girl-4.1.0/lib/factory_girl/syntax/default.rb:6:in `define’: wrong number of arguments (1 for 0) (ArgumentError) from /Users/gideon/Sites/testing/spec/factories.rb:1:in `’ 以下是涉及的文件…… login_user_spec.rb require_relative ‘../spec_helper’ require_relative ‘../lib/login_user’ describe “Existing User Log in Scenario”, :type => :request do before :all do @user = FactoryGirl(:user) end xit “should allow user to select login from top nav” do visit […]

嵌套模型测试:找不到表’*’错误。

我正在尝试对一个working大型代码库运行RSpec(我对Rails相对较新),但它在这一点上失败了; 我敢打赌它与FactoryGirl定义有关。 模型概述: class User {:approved => true} has_many :friendships, :class_name => “User”, :source => :friend, :through => :friends # … 测试方法: # models/user.rb def add_friend(user_id, friend_id) @friendship = self.friends.new({:user_id => user_id, :friend_id => friend_id}) return false unless @friendship.save end FactoryGirl工厂: FactoryGirl.define do factory :user, :class => User do |f| # … end factory :friend, […]

Rails FactoryGirl内部应用程序在开发环境中

我正在尝试使用rails_email_preview gem在我的应用程序中使用FactoryGirl gem开发模式(它用于邮件程序测试更多 )。 它可以工作,但只在初始页面加载,重新加载/刷新页面后,我得到以下错误: Factory not registered: order 其中order是工厂名称。 这是我的代码(它是我的Rails引擎的虚拟应用程序): 规格/虚拟/应用/ mailer_previews / mymodule中/ order_mailer_preview.rb Dir[Mymodule::Core::Engine.root.join(‘spec’, ‘factories’, ‘*’)].each do |f| require_dependency f end module Mymodule class OrderMailerPreview def confirmation_email o = FactoryGirl.create(:order) OrderMailer.confirmation_email(o) end end end 当然,我的工厂在测试环境中没有任何问题。 任何帮助,将不胜感激。 编辑: p FactoryGirl.factories 返回(页面重新加载后) #

使用FactoryGirl构建json存根

我尝试使用工厂构建一个json ,但是当我尝试build它时它是空的。 以下是Factory类。 require ‘faker’ FactoryGirl.define do factory :account do |f| f.name {Faker::Name.name} f.description {Faker::Name.description} end factory :accjson, class:Hash do “@type” “accountResource” “createdAt” “2014-08-07T14:31:58” “createdBy” “2” “disabled” “false” end end 以下是我试图建立的方式。 hashed_response = FactoryGirl.build(:accjson) expect(account.to_json).to eq(hashed_response.to_json); 但我的hashed_response似乎总是空object 。

你如何使用Fabrication定义特征

我正在学习如何在Rails使用制造 ,我们决定用制造代替所有的factory_girl代码。 假设我们在factory_girl有这个代码 FactoryGirl.define do factory :user do trait(:no_credits) { credits 0 } trait(:with_credits) { credits 300 } 你将如何在Fabrication中定义这个? 我已经浏览了他们的网站,但找不到任何相关的内容。 非常感谢您的帮助

我们应该在Rails工厂中使用Faker吗?

我喜欢Faker ,我在我的seeds.rb使用它seeds.rb一直用真实的数据来填充我的开发环境。 我也刚刚开始使用Factory Girl ,这也节省了大量时间 – 但是当我在网络上搜索代码示例时,我没有看到很多人将两者结合起来的证据。 问:为什么人们不在工厂里使用faker有充分的理由吗? 我的感觉是,通过这样做,我会通过随机播种随机 – 但可预测的 – 数据来增加我的测试的稳健性,这有望增加错误弹出的可能性。 但也许这是不正确的,或者对硬编码工厂没有任何好处,或者我没有看到潜在的陷阱。 是否应该或不应该合并这两颗gem有充分的理由吗?

使用不带Rails的FactoryGirl,ActiveRecord或任何带RSpec的数据库

我想知道是否有人知道在没有任何上述先决条件的情况下是否可以使用FactoryGirl。 我想用它来为移动和网络甚至可能的API驱动UI自动化测试时生成动态测试数据。 我知道我可以创建一些自定义助手类/方法并使用getter和setter等,但我认为使用这个很棒的小gem会很好。 我搜索得相当广泛,并试图建立一个基本的RSpec项目(我也尝试过Cucumber),但无济于事。 我似乎仍然需要使用相关登录实例化类以便使用它。 FactoryGirl.define do factory :user do firstname { Faker::Name.first_name } lastname { Faker::Name.last_name } age { 1 + rand(100) } end end 然后,如果我尝试在RSpec文件中调用它… describe… it… user = build_stubbed(:user) end end 我还阅读了文档并尝试了所有其他变体,我只是继续… Failure/Error: user = build_stubbed(:user) NameError: uninitialized constant User 这表明我需要一个名为User的类,包含所有逻辑。

FactoryBot工厂中`transient do`块的目的是什么?

在FactoryBot工厂中进行transient do的目的是transient do ? 我见过很多以下面的东西开头的工厂。 factory :car do owner nil other_attribute nil end … 我在这个博客上找到了一些信息: http : //blog.thefrontiergroup.com.au/2014/12/using-factory-easily-create-complex-data-sets-rails/ 但我仍然不完全明白如何以及为什么这样做。 我对FactoryBot的体验很小。 任何有使用FactoryBot经验的人都可以分享一些见解吗?