factory-bot

Factory-bot - How to build association and nested attributes

China☆狼群 提交于 2019-12-24 07:06:32
问题 I am new to Factories and I need help for the association and nested attributes.... How do I set an admin user that creates a product? OK How do I set category to a product? Ok How do I attach images to a product? OK How do I set product's sizes (nested attibutes) user.rb has_many :products product.rb belongs_to :user belongs_to :category has_many :sizes, inverse_of: :product, dependent: :destroy #nested_attributes size.rb belongs_to :product category.rb has_many :products factories/users.rb

How to test a nested form with RSpec and FactoryGirl?

萝らか妹 提交于 2019-12-24 05:38:09
问题 In my Rails project I have a User that can have many Projects which in turn can have many Invoices . Each Invoice can have many nested Items . class Invoice < ActiveRecord::Base attr_accessible :number, :date, :recipient, :project_id, :items_attributes belongs_to :project belongs_to :user has_many :items, :dependent => :destroy accepts_nested_attributes_for :items, :reject_if => :all_blank, :allow_destroy => true validates :project_id, :presence => true def build_item(user) items.build(:price

FactoryGirl factory with Trait(s) that returns a Hash with stringed keys

夙愿已清 提交于 2019-12-24 02:13:49
问题 I'm trying to use FactoryGirl to build a Hash that returns something like this: => {"3"=>"1", "6"=>"Word"} I'm getting close but not 100% there yet... The first factory definition i tried looked like this: factory :faqtory, class: Hash do |f| f.ignore do fake_word Faker::Lorem.word end f.sequence(1.to_s) { |n| n } f.send(2.to_s, fake_word.to_s.capitalize) initialize_with { attributes.stringify_keys } end Unfortunately this doesn't work: 1.9.3p448 :001 > FactoryGirl.build :faqtory

RSpec and Factory for testing next, prev records

China☆狼群 提交于 2019-12-24 00:51:46
问题 I have a User model which has methods for a next and previous 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 I'm trying to setup a test using RSpec and Factory girl to test this, and am not sure of the approach - i'm only getting started with TDD. I'm trying this: describe "previous_next" do let(:user) { FactoryGirl.create(:user) } let(:next_user) { FactoryGirl.create(:user) } it "should know the next

“Invalid gemspec” messages during “bundle install” of factory girl (rails 3.1 and ruby 1.9.2p290)

孤者浪人 提交于 2019-12-23 17:18:51
问题 Note: I asked this question when I got unexpected messages when doing bundle install with the factory_girl gem using Ubuntu 11.10. Being a noob I had ... and still have ... no idea what was going on. The update below attempts to outline what I did to eventually get rid of the messages. I am leaving the question around with a slightly more pertinent title line in case it might be of use to someone else. I look at Factory Girl's README.md and I see: Install gem install factory_girl or add the

Factorybot - How to set Nested attributes

有些话、适合烂在心里 提交于 2019-12-23 17:00:16
问题 I beleive it's better to create a new question... It follows my previous question my model product has many sizes (nested attributes) I want to create Factories but I can't make it work... A product is valid if it has at least one size ( size_name and quantity ) FactoryBot.define do factory :product do title { Faker::Artist.name} ref { Faker::Number.number(10)} price { Faker::Number.number(2) } color { Faker::Color.color_name } brand { Faker::TvShows::BreakingBad } description { Faker::Lorem

How to test upload with Carrierwave + FactoryGirl

為{幸葍}努か 提交于 2019-12-23 15:30:01
问题 I want to create some tests for my app and I have the following error: 1) User feeds ordering should order feeds by id desc Failure/Error: @post_1 = FactoryGirl.create(:post) ActiveRecord::AssociationTypeMismatch: Attachment(#87413420) expected, got Rack::Test::UploadedFile(#81956820) # ./spec/models/user_spec.rb:37:in `block (3 levels) in <top (required)>' This error is because I have this on my factories.rb file factory :post do title "Lorem Ipsum" description "Some random text goes here"

How to stub gmaps4rails geocode functions in rspec tests?

爱⌒轻易说出口 提交于 2019-12-23 09:59:49
问题 I'm using gmaps4rails, and trying to develop some tests. I have a factory factory :country do sequence(:name) { |n| "Country#{n}" } end which is obviously not recognized by Google. Validation failed: Gmaps4rails address Address invalid The API calls are also taking time to run in my tests. How can I stub the API call out? I've tried adding before(:each) Country.stub(:geocode) end to the spec file but it has no effect. My model looks like this class Country < ActiveRecord::Base acts_as

Rails 3 Factory Girl + Many to Many Relationships

不羁岁月 提交于 2019-12-23 09:34:06
问题 There aren't currently any up to date answers for this using Factory Girl 4.1 (that I could find) - how do you setup a many to many relationship inside of a factory? For instance I have Students and Classrooms which are in a many to many relationship using a join table, so far I had the following setup: factory :classroom do name "Foo Class" ... end factory :student do name "John Doe" ... end factory :student_with_classroom, :parent => :student do after(:build) {|student| student.classrooms <

FactoryGirl override attribute of associated object

て烟熏妆下的殇ゞ 提交于 2019-12-23 06:47:09
问题 This is probably silly simple but I can't find an example anywhere. I have two factories: FactoryGirl.define do factory :profile do user title "director" bio "I am very good at things" linked_in "http://my.linkedin.profile.com" website "www.mysite.com" city "London" end end FactoryGirl.define do factory :user do |u| u.first_name {Faker::Name.first_name} u.last_name {Faker::Name.last_name} company 'National Stock Exchange' u.email {Faker::Internet.email} end end What I want to do is override