Tag: factory bot

在FactoryGirl中查找_or_initialize_by

我想知道FactoryGirl中是否存在find_or_initialize_by的等价物来解决以下问题: 目标是该模型使用两个具有相同国家/地区的表。 我不想在国家使用序列(正如我在电子邮件中找到的那样)。 Country上有一个唯一性约束,但我的主要问题是,当我调用FactoryGirl.create(:click)时,它会创建两次相同的Country记录 因此,validation在测试中失败。 Rspec的: # models/click_spec.rb describe Click do it “should have a valid constructor” do FactoryGirl.create(:click).should be_valid end end 工厂: # factories/countries.rb FactoryGirl.define do factory :country do name “United States” slug “us” end end # factories/offers.rb FactoryGirl.define do factory :offer do association :country, factory: :country # Other columns end end # factories/users.rb […]

FactoryGirl – validation失败:时区未包含在列表中

我正在使用Rspec,FactoryGirl和Capybara。 使用ActiveSupport :: TimeZone.us_zones时。 我为我的requests/users_spec运行我的测试,它甚至没有达到测试,因为它在工厂有问题。 这是错误: Failure/Error: make_user_and_login ActiveRecord::RecordInvalid: Validation failed: Time zone is not included in the list # ./spec/support/user_macros.rb:3:in `make_user_and_login’ # ./spec/requests/users_spec.rb:7:in `block (3 levels) in ‘ 这是我的设置: 应用程序/模型/ user.rb class User /^(?=(.*[a-zA-Z]){3})[a-zA-Z0-9]+$/ validates_uniqueness_of :email validates_uniqueness_of :username, :case_sensitive => false validates_length_of :username, :within => 3..26 validates_inclusion_of :time_zone, :in => ActiveSupport::TimeZone.us_zones end 投机/ spec_helper.rb […]

使用factory_girl进行RSpec测试,并使用will_paginate进行关联对象

我在rspec中为will_paginate编写测试时遇到问题。 问题是我有一个拥有许多宠物的主人的模型。 这导致一个看起来像这样的factories.rb文件: Factory.define :owner do |owner| owner.personid “1111111” owner.firstname “Nisse” owner.lastname “Gunnarsson” owner.street “Street” owner.postaladress “38830” owner.town “Town” owner.phone “555-5555” owner.mobile “555-5556” owner.email “nisse@test.com” owner.reminder true end Factory.define :pet do |pet| pet.name “Hedvig” pet.specie “Rabbot” pet.breed “Lowen/vadur” pet.colour “Madagaskar” pet.association :owner end 在我的测试中,我有 describe “Get show” do before(:each) do @owner = Factory(:owner) 30.times do […]

使用FactoryGirl时,Paperclip :: Attachment作为字符串传递

我正在尝试使用FactoryGirl 3.0在rails 3.2.6上创建回形针3.1.2的function测试,当我传递文件附件时,我的控制器接收一个字符串,它是文件位置而不是Paperclip :: Attachment对象。 我的工厂是: include ActionDispatch::TestProcess FactoryGirl.define do factory :artist do id 1 name ‘MyString’ photo {fixture_file_upload(“#{Rails.root}/test/fixtures/files/rails.png”,’image/png’)} end end 我的测试是: test “should create artist” do assert_difference(‘Artist.count’) do post :create, :artist => { name: @artist.name, photo: @artist.photo }, :html => { :multipart => true } end assert_redirected_to artist_path(assigns(:artist)) end 在我的控制器中: params[:artist][:photo].class.name 等于“字符串”,但是当我将它添加到测试时,这会通过 assert @artist.photo.class.name […]

使用具有Rspec视图的Factory Girl

所以我想使用Rspec和Factory Girl测试一些视图,但我不知道如何正确地将工厂分配给视图。 我在网上看到的所有信息都使用了存根,虽然存根很好,但我想保持我的rspec内容有些一致。 我想使用工厂作为版本模型,并在渲染页面时将其与页面关联,但我考虑过的所有内容似乎都被打破了。 这是一个荒谬的例子: require ‘spec_helper’ describe “catalog/editions/rationale.html.haml” do before do @edition = Factory.create(:edition) assign(:editions, @edition) render :action => ‘rationale’, :id => @edition end context “GET rationale” do it “should not have the preamble to the United States constitution” do rendered.should_not contain(/We the People of the United States/) end end end 在这里我尝试改变渲染:action =>’rationale’,:id => […]

RSpec和Factory用于测试下一个,prev记录

我有一个User模型,它有下一个和上一个用户的方法: def next_user User.where(“id > ?”, id).order(“id ASC”).first end def prev_user User.where(“id < ?", id).order("id DESC").first end 我正在尝试使用RSpec和Factory girl设置测试来测试这个,并且我不确定这个方法 – 我只是开始使用TDD。 我正在尝试这个: describe “previous_next” do let(:user) { FactoryGirl.create(:user) } let(:next_user) { FactoryGirl.create(:user) } it “should know the next user” do expect(user.next_user).to eq next_user end it “should know the previous user” do expect(next_user.prev_user).to eq user end […]

何时在BDD循环中从黄瓜切换到rspec以进行登录过程

我还在尝试理解BDD循环中cucumber和rspec的组合。 我为一个非常简单的登录系统定义了以下场景: Feature: Log in In order to get access to the application As a user I want to log in Scenario: User logs in successfully Given I exist as a user When I go to the login page And I fill in “username” with “gabrielhilal” And I fill in “password” with “secret” And I […]

工厂女孩有很多关系(和受保护的属性)

我有这种关系: class Article < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :article attr_protected :article_id end 控制器内部的默认方案如下: @article = Article.create(:title => “foobar”) @comment = @article.comments.create(:content => “w00t”) 我试过写这些工厂: Factory.define :article do |f| f.title “Hello, world” end Factory.define :comment do |f| f.content “Awesome!” f.association :article end 但我的语法关联不正确。 由于评论的article_id protected属性,这有点棘手。 所以我认为如果我在文章工厂内声明关联应该会更好,但我不知道如何处理。 谢谢你的帮助。

如何使FactoryGirl.create影响另一个记录的属性?

在order.save商店应用程序中,通常在预订产品之后, bookings_controller创建操作会执行order.save ,而order.save又会激活必要的before_save方法order.sum_of_all_bookings 。 在为查看订单列表的管理员构建RSpec测试时,进行预订不会更改订单总数。 索引在浏览器中有效。 require ‘rails_helper’ RSpec.feature ‘Admin can oversee orders’ do let(:admin) { FactoryGirl.create(:user, :admin) } let(:customer) { FactoryGirl.create( :user ) } let!(:order_1) { FactoryGirl.create( :order, customer: customer ) } let!(:booking_1) { FactoryGirl.create( :booking, product_name: ‘Honingpot’, product_quantity: 1, product_price: ‘5,00’, order: order_1 ) } let!(:order_2) { FactoryGirl.create( :order, customer: customer ) } […]

使用OmniAuth在Factory Girl中创建用户?

我目前正在创建一个使用OmniAuth创建和validation用户的应用程序。 我在测试期间遇到问题,因为Factory Girl无法在没有OmniAuth的情况下生成用户。 我有几种不同的方法让工厂女孩用omniauth创建用户,但没有一个成功。 我在spec_helper文件中添加了以下两行 OmniAuth.config.test_mode = true \\ allows me to fake signins OmniAuth.config.add_mock(:twitter, { :uid => ‘12345’, :info => { :nickname => ‘Joe Blow’ }}) 目前的工厂.rb FactoryGirl.define do factory :user do provider “twitter” sequence(:uid) { |n| “#{n}” } sequence(:name) { |n| “Person_#{n}” } end end 以下测试当前失败,因为没有生成用户 let(:user) { FactoryGirl.create(:user) } before { sign_in […]