How come Factory Girl isn't sequencing unique attributes?
Asked Answered
C

3

11

My controller spec fails because Factory Girl seems to be creating non-unique Users even though I sequence the User attributes that need to be unique.

The Errors

  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 has already been taken

The Controller Spec (RSpec)

  describe "POST #create" do                          
    let(:valid_topic) {Factory.build :topic}
    let(:invalid_topic) {Factory.build :invalid_topic}

    context "when topic is invalid" do
      it "should render new" do
        post :create, :topic => invalid_topic
        response.should render_template(:new)
      end
    end
    context "when topic is valid" do
      it "should redirect to show" do
        post :create, :topic => valid_topic
        response.should redirect_to(topic_path(assigns(:topic)))
      end
    end
  end

The Factories

Factory.define :user do |f|
  f.sequence(:username) { |n| "foo#{n}"}
  f.password "password"
  f.password_confirmation { |u| u.password}
  f.sequence(:email) { |n| "foo#{n}@example.com"}
end

Factory.define :topic do |f|
  f.name "test topic"
  f.association :creator, :factory => :user
  f.forum_id 1
end

Why isn't Factory Girl sequencing the User attributes when I use Factory.create :topic?

Cockleboat answered 22/5, 2011 at 21:51 Comment(1)
Run rake db:test:prepare. Run the specs, do they fail? Run them again, do they fail now?Bathesda
C
29

rake db:test:prepare seemed to fix the problem.

Not sure why, though. The schema hadn't been changed.

Cockleboat answered 22/5, 2011 at 22:2 Comment(3)
Thanks for providing the solution to your question. I was having the same issue, and couldn't figure out why since FactoryGirl was properly sequencing.Faraday
db:test:prepare empties the database. FactoryGirl's sequence is test-run-specific -- it starts from 1 (0?) every time you run the specs. This error happens when data gets left behind after tests either because they were stopped improperly or don't clean up after themselves (you'll get this problem systemically if you put factory create calls in a before all without cleaning up in an after all)Unspent
You should run this with Bundler: bundle exec rake db:test:prepareDonohoe
A
4

Please, consider using database_cleaner gem. One was designed specifically to fulfill the purpose of cleaning up database between test runs.

This post explains pretty much everything.

Agglutinate answered 24/9, 2012 at 6:54 Comment(0)
S
3

You should consider deleting all topics by hand in the end of the test. Of course, it is not number one solution but worked out for me real great.

after(:all) { Topic.delete_all }
Starrstarred answered 17/9, 2012 at 16:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.