factory-bot

FactoryGirl screws up rake db:migrate process

北慕城南 提交于 2019-12-17 15:28:03
问题 I am doing TDD/BDD in Ruby on Rails 3 with Rspec (2.11.0) and FactoryGirl (4.0.0). I have a factory for a Category model: FactoryGirl.define "Category" do factory :category do name "Foo" end end If I drop, create then migrate the database in the test enviroment I get this error: rake aborted! Could not find table 'categories' This problem occurs because FactoryGirl expects the tables to already exist (for some odd reason). If I remove the spec folder from my rails app and do db:migrate , it

Rail 3.2.2/Devise: deprecation warning with rspec

微笑、不失礼 提交于 2019-12-14 00:29:16
问题 I recently upgraded an app to rails 3.2.2. I'm using Factory_girl Factory.sequence :name do |n| "name-#{n}" end Factory.define :user do |u| u.first_name{ Factory.next(:name) } u.last_name { |u| 'last_' + u.first_name } u.password 'secret' u.password_confirmation { |u| u.password } u.sequence(:email) { |i| "user_#{i}@example.com" } end and this simple test specify { Factory.build(:user).should be_valid } generate the following warning DEPRECATION WARNING: You're trying to create an attribute

Paperclip::Attachment passed as string when using FactoryGirl

旧城冷巷雨未停 提交于 2019-12-13 19:13:29
问题 I am trying to create a functional test for paperclip 3.1.2 on rails 3.2.6 using FactoryGirl 3.0 and when i pass the file attachment my controller receives a string which is the file location and not the Paperclip::Attachment object. My factory is: 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 And my test is: test "should create artist" do assert

Factory Girl error with has_many relationship

天大地大妈咪最大 提交于 2019-12-13 12:26:45
问题 I have the following factories: Factory.define :email do |email| email.email {"infomcburney.cowan.com"} end Factory.define :lead do |lead| lead.emails {|emails| [emails.association(:email)]} end Which are modeling the following classes class Lead < ActiveRecord::Base has_many :emails end class Email < ActiveRecord::Base belongs_to :lead, :class_name => "Lead", :foreign_key => "lead_id" end When I run the this test through shoulda: should "capture emails" do lead = Factory.build(:lead) assert

Rails 4, RSpec, MySQL2 default value error on first factory only

别说谁变了你拦得住时间么 提交于 2019-12-13 12:22:53
问题 I'm upgrading from Rails 3.2.14 to Rails 4. When running RSpec on my test suite in my Rails 3 branch, all tests pass. On the Rails 4 branch, however, I'm getting an error on the first instance only of creating a factory. The error reads: ActiveRecord::StatementInvalid: Mysql2::Error: Field 'id' doesn't have a default value: INSERT INTO `of_of_measurements` (`aerobic_steps`, `calories`, `date`, `device_serial`, `distance`, `is_device_input`, `server_time`, `total_steps`, `user_id`) VALUES (15,

Rails/Devise - What should I test with devise and rspec?

北城以北 提交于 2019-12-13 12:11:25
问题 Many programmers use devise as their authentication solution and I would like to get their advice: Devise is already tested, but I want to know if there is something to test by myself (integration/unit/funtionnal tests?) for a standard devise integration with my knowledge (I'm not familiar with shoulda and cucumber, but I know a bit of rspec and factory girls) Thanks for your advices!! 回答1: Testing the integration can be great. Because is how you integrate devise where you can do some error.

'Factory not registered' error in Rails 4

拥有回忆 提交于 2019-12-13 07:36:26
问题 I am getting a 'Factory not registered' error using Factory Girl in Rails 4. Here is my factory definition for a simple Devise user in spec/factories/user.rb: FactoryGirl.define do factory :user do email 'test@example.com' password 'foobar' password_confirmation 'foobar' end end Here is the 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

'cannot load such file — factory_girl_rails (LoadError)' in rails 4.1.8

ⅰ亾dé卋堺 提交于 2019-12-13 06:38:06
问题 While running rspec, I am getting the following error: /2.2.0/rubygems/core_ext/kernel_require.rb:54:in require': cannot load such file -- factory_girl_rails (LoadError) from /Users/radhikabhatt/.rbenv/versions/2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:inrequire' from /Users/radhikabhatt/Desktop/work/cl_portalmiudla/spec/spec_helper.rb:19:in <top (required)>' from /Users/radhikabhatt/.rbenv/versions/2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb

Nested models testing : Could not find table '*' Error.

依然范特西╮ 提交于 2019-12-13 01:30:33
问题 I'm trying to run RSpec against a working large codebase (I'm relatively new to Rails), but it fails on this point; My bet that it has something to do with the FactoryGirl definitions. Overview of the model : class User < ActiveRecord::Base # ... has_many :friends, :conditions => {:approved => true} has_many :friendships, :class_name => "User", :source => :friend, :through => :friends # ... The method to test : # models/user.rb def add_friend(user_id, friend_id) @friendship = self.friends.new

Factory Girl uninitialized constant Model Name with rails-api

允我心安 提交于 2019-12-13 00:29:29
问题 I'm trying to make a factory for my User model, but when I run rspec, I get Failure/Error: user = build(:user, email: "invalid@email.com") NameError: uninitialized constant User When I hop into rails console, I can do User.all and it will return the users I have and FactoryGirl.build(:user, email: "invalid@email.com") works just fine, so I'm not sure where the problem is. I've created the project with rails-api, using rails 4.2 ruby 2.2 # spec/spec_helper.rb # i have to set spec/factories as