factorygirl

Factory Girl sequence fails under autospec

I have this Factory: Factory.define :email_address do |e| e.sequence(:address) { |n| "factory_#{n}@example.com" } e.validated true end When I run my specs with rake spec, it works fine. When I run autospec, it fails right away, claiming that the email address is being used twice in two different objects (there is a validation whi...

Action works, but test doesn't (Shoulda)

I am trying to test my update action in Rails with this: context "on PUT to :update" do setup do @countdown = Factory(:countdown) @new_countdown = Factory.stub(:countdown) put :update, :id => @countdown.id, :name => @new_countdown.name, :end => @new_countdown.end end should_respond_with :redirect should_redirect_to(...

Passing single attributes to associated factories

I'm looking for a way to pass fields into the factories of associated models without having to explicitly call the factory itself. For example, lets say I have some factories like so: Factory.define :person do |person| person.name "Default Bob" person.sex "M" person.house {|p| p.association(:house)} end Factory.define :house ...

factory girl: association problem testing model which has validates_presence_of accepts_nested_attributes_for

hey, i have a simple associations: class Account < ActiveRecord::Base has_many :users accepts_nested_attributes_for :users validates_presence_of :users end and class User < ActiveRecord::Base belongs_to :account end i just want to run a simple test: describe 'a new', Account do it 'should be valid' do Factory.buil...

Testing models with relationships and callbacks in Rails with RSpec and Factory_Girl

I'm still a working on learning RSpec so I'm sorry if completely overlooked something... I'm writing a test for a recipe that has many ingredients. The ingredients are actually added as a percent (with a total % column on the formulation) so I want to make sure that the total column updates after every save. So right now my RSpec test...

Cucumber features failing "undefined method `Factory'" with bundler, rails 2.3.5

rake cucumber --trace output: /usr/bin/ruby1.8 -I "/home/vadim/.bundle/ruby/1.8/gems/cucumber-0.8.3/lib:lib" "/home/vadim/.bundle/ruby/1.8/gems/cucumber-0.8.3/bin/cucumber" --profile default Using the default profile... ... Given I signed up with "username/[email protected]/password" # features/step_definitions/clearance_steps.rb:13 ...

Creating instances with unique attributes using Factory Girl

I have a constraint and a validation placed on the guid field so that each is unique. The problem is, with the factory definition that I have below, I can create only one user instance, as additional instances fail validation. How do I do this correctly so that the guid field is always unique? Factory.define(:user) do |u| u.guid UU...

How Do I Use Factory Girl To Generate A Paperclip Attachment?

I have model Person that has many Images, where images has a Paperclip attachment field called data, an abbreviated version displayed below: class Person has_many :images ... end class Image has_attached_file :data belongs_to :person ... end Person is required to have at least one Image attached to it. When using FactoryGi...

rspec association test only works in one direction

I upgraded to Rails 3 and RSpec 2 and one of my RSpec tests stopped working: # Job.rb class Job < ActiveRecord::Base has_one :location belongs_to :company validates_associated :location end # Location.rb class Location < ActiveRecord::Base belongs_to :job end # job_spec.rb describe Job, "location" do it "should have a ...

factory_girl + rspec doesn't seem to roll back changes after each example

Similar to the problem described here: http://rpheath.com/posts/411-how-to-use-factory-girl-with-rspec in Short (shorten'd code): spec_helper: config.use_transactional_fixtures = true config.use_instantiated_fixtures = false factories.rb: Factory.define :state do f.name "NY" end in my spec before(:each) do @static_model = ...

How can I reset a factory_girl sequence?

Provided that I have a project factory Factory.define :project do |p| p.sequence(:title) { |n| "project #{n} title" } p.sequence(:subtitle) { |n| "project #{n} subtitle" } p.sequence(:image) { |n| "../images/content/projects/#{n}.jpg" } p.sequence(:date) { |n| n.weeks.ago.to_date ...

How can I build/create a many-to-many association in factory_girl?

I have a Person model that has a many-to-many relationship with an Email model and I want to create a factory that lets me generate a first and last name for the person (this is already done) and create an email address that is based off of that person's name. Here is what I have for create a person's name: Factory.sequence :first_name ...

Helper functions in Rails unit tests

I'm using this sort of code in my code in my Unit tests. test "should be awesome" do assert true end I'm using FactoryGirl instead of fixtures. I find that I'm repeating myself a lot and having helper functions would be quite useful. What's the best way to create and call a helper function in the unit test? Is there a before_filt...

rspec - paperclip not loading attr_accessors

I'm using Rails 2.3.5, paperclip 2.3.3 with rspec and factory_girl. Can't figure out why the attr_accessors are undefined when I try to test upload or simply calling. e.g. account.profile_photo.file? undefined method `profile_photo_file_name' for #Account:0x105970af8 Where I have it running properly in Development. account.rb h...

Why isn't factory_girl operating transactionally for me? - rows remain in database after tests

I'm trying to use factory_girl to create a "user" factory (with RSpec) however it doesn't seem to be operating transactionally and is apparently failing because of remnant data from previous tests in the test database. Factory.define :user do |user| user.name "Joe Blow" user.email "[email protected]" ...

What's wrong with this Factory Girl definition

#test/factories.rb Factory.define :estado do |estado| estado.nombre "Distrito Federal" end Factory.define :municipio do |municipio| municipio.nombre "Cuauhtémoc" municipio.estado { |estado| estado.association(:estado) } end Factory.define :colonia do |colonia| colonia.nombre "Condesa" colonia.municipio { |municipio| municipi...

Rails 3 and Factory Girl creating user only if attributes are passed in

Hey, I'm using Rails 3 rc, Factory Girl, and Rspec, and Authlogic. Is there any way or reason why this would happen: When I create a user like this: @user = Factory(:user) I get an issue with password confirmation being "too short". my factories.rb is Factory.define :user do |u| u.username "Test User" u.email "...

Setting up factory_girl with internal array in rails

Hi, I have the following shoulda set up which I would like to use factory_girl to have the fake model available in other specs. So far I have this: context "on POST to parse" do setup do @fuzzy_lead = LeadCapturer::FuzzyLead.new parts = LeadCapturer::LeadPartArray.new parts << LeadCapturer::LeadPart.new('Mc B...

Factory Girl with has many relationship (and a protected attribute)

Hello, I have this kind of relation: class Article < ActiveRecord::Base has_many :comments end class Comment < ActiveRecord::Base belongs_to :article attr_protected :article_id end The default scenario inside controllers looks like: @article = Article.create(:title => "foobar") @comment = @article.comments.create(:content => ...

Accessing child attributes from parent Factory Girl factories

I'm implementing Factory Girl as a replacement for fixtures in my Rails app. I have several tables that I'm trying to represent using associations. However, to throw a kink into the loop, beyond just defining the associations, I also need to access attributes of the child factories from the parent. Below is an example of what I'm tryi...