Tag: factory bot

使用Rspec测试“Post create”

我正在尝试用Rspec测试“Post create”动作。 代码如下: def valid_attributes { :zone => Flymgr::Zone.new(:countries => Flymgr::ZoneCountry.first, :name => ‘USA’, :description => ‘USA Flight’, :zipcodes => ”), :price => ‘100.00’, :class => ‘first’, } end def valid_session {} end before(:each) do @request.env[“devise.mapping”] = Devise.mappings[:admin] admin = FactoryGirl.create(:admin) sign_in admin end describe “POST create” do describe “with valid params” do it “creates […]

Factory Girl / Capybara从数据库测试中删除记录?

与RSpec和Capybara合作,我得到了一个有趣的测试失败模式,在测试用例中有一些细微的重排线……这些都不重要。 我正在开发自己的身份validation系统。 它目前正在工作,我可以登录/退出浏览器和会话工作等等。但是,尝试测试这是失败的。 正在发生的事情我不太明白,这似乎取决于(看似)不相关的电话的顺序。 require ‘spec_helper’ describe “Sessions” do it ‘allows user to login’ do #line one user = Factory(:user) #For SO, this method hashes the input password and saves the record user.password! ‘2468’ #line two visit ‘/sessions/index’ fill_in ‘Email’, :with => user.email fill_in ‘Password’, :with => ‘2468’ click_button ‘Sign in’ page.should have_content(‘Logged in’) end […]

Factory Girl怎么没有对独特属性进行排序?

我的控制器规范失败,因为Factory Girl似乎正在创建非唯一用户,即使我对需要唯一的用户属性进行排序。 错误 1) TopicsController POST #create when topic is invalid should render new Failure/Error: let(:invalid_topic) {Factory.build :invalid_topic} ActiveRecord::RecordInvalid:Validation failed: Email has already been taken, Username has already been taken 2) TopicsController POST #create when topic is valid should redirect to show Failure/Error: let(:valid_topic) {Factory.build :topic} ActiveRecord::RecordInvalid: Validation failed: Email has already been taken, Username […]

如何在factory_girl工厂中包含一个模块?

我正在尝试在我的所有工厂中重用一个辅助方法,但是我无法让它工作。 这是我的设置: 辅助模块(在spec / support / test_helpers.rb中) module Tests module Helpers # not guaranteed to be unique, useful for generating passwords def random_string(length = 20) chars = [‘A’..’Z’, ‘a’..’z’, ‘0’..’9′].map{|r|r.to_a}.flatten (0…length).map{ chars[rand(chars.size)] }.join end end end 工厂(在spec / factories / users.rb中) FactoryGirl.define do factory :user do sequence(:username) { |n| “username-#{n}” } password random_string password_confirmation { […]

rspec,factory_girl和datamapper的spork和cache_classes问题

我有Spork测试服务器的问题。 如果我在config / environments / test.rb中设置config.cache_classes = false,那么规格就会开始出现rasie错误。 Failure/Error: task = Factory(:something, :foo => @foo, :bar => @bar) DataMapper::ImmutableError: Immutable resource cannot be modified 这是我的spec_helper.rb: require ‘spork’ Spork.prefork do if ENV[‘CODE_COVERAGE’] == ‘1’ require ‘simplecov’ SimpleCov.start ‘rails’ end ENV[“RAILS_ENV”] ||= ‘test’ require File.expand_path(“../../config/environment”, __FILE__) require ‘rspec/rails’ require ‘webmock/rspec’ require ‘factory_girl’ Dir[Rails.root.join(“spec/controllers/shared/*.rb”)].each { |f| require f […]

为什么运行Rspec时会出现未定义的方法’have’错误?

我最近升级到Rails 4,一切正常,除了我的Rspec测试。 require ‘spec_helper’ describe Invoice do before :each do @user = FactoryGirl.create(:activated_user) person = FactoryGirl.create(:person, :user => @user, :company => nil) @project = FactoryGirl.create(:project, :user => @user, :person_ids => [person.id], :invoice_recipient_id => person.id) end it “has a valid factory” do expect(FactoryGirl.build(:invoice, :project => @project, :user => @user)).to be_valid end it “is invalid without a […]

带载波上传字段的工厂

你好,我需要为我的模型建立工厂,例如 Factory.define :farm do |f| f.name { Factory.next :name } f.harvest ‘3’ f.offers ‘Random’ f.latitude ’43’ f.longitude ‘-70’ f.about ‘We rocks!’ f.logo { Factory.next :logo } # this doesn’t work end 现在我只是将字符串“#{n} .jpg”传递到我的徽标字段中,这不起作用,如何评估此字段? 我正在使用CarrierWave进行上传。

如何制作has_many:通过与灯具的关联?

我不能使用factory_girl因为我正在测试太阳黑子,需要真正的数据库。 编辑 :不。 它可以与太阳黑子一起使用。 我错了。 如何在灯具中构建has_many:through(又名多对多)关联? 我谷歌并得到一个无效的解决方案 编辑 : 最后我使用factory_girl。 我google-copy-paste一个片段: factory :tagging do question { |a| a.association(:question) } tag { |a| a.association(:tag) } end (问题has_many标签通过标签,反之亦然) 它运作良好。 但它是什么? factory_girl的自述文件并不意味着这种语法。 有人能解释一下吗

RSpec和数据库清理程序 – 将某些对象永久保留在测试数据库中

我(姗姗来迟)开始使用RSpec / capybara测试我的Rails应用程序(购物网站),使用数据库清理程序清除数据库,并使用Factory Girl为每个测试生成新对象(像大多数人一样)。 这很好用,我认为清除测试之间的数据是个好主意。 但是,它可能会变慢并且(据我所知)生成同一对象的多个实例有点乏味。 在我的数据库中有一些对象总是相同的,或者我将始终为测试生成相同的副本。 例如,我的Package模型,它定义订阅包的定价和function限制。 它可能永远不会改变。 有没有办法,使用此配置(请注释并指定是否需要更多信息),将某些对象实例放入测试数据库并将其从数据库清理器中排除 ,或以任何其他方式保留特定对象的永久副本测试数据库? 这主要是为了提高测试速度。

FactoryGirl瞬态变量的未定义方法

我在version 4.4使用FactoryGirl 。 我正在尝试创建after(:create)回调,但我在评估变量上失败了。 FactoryGirl.define do factory :organization do name ‘example’ factory :organization_with_links do transient do links_count 5 end after(:create) do |organization, evaluator| create_list(:link, evaluator.links_count, organization: organization) end end end end 不幸的是我得到了 NoMethodError: undefined method `links_count’ for FactoryGirl::SyntaxRunner:0x007fcefb1ff780> 根据官方工厂女孩指南我正在做的一切正确: https : //github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md#associations 任何建议如何使transient variable工作?