kc00l's solution to "Rspec tests fail in Guard but manually running rspec test passes. Why?"

This spec fails because of how FactoryGirl generates a name sequence for your Standard factory. When you run the spec manually the counter for the sequence starts at 1 but it is not reset when you run rspec through guard. It is a common issue and it's caused by our blind trust of a determinate state of the test database in specs. Been there, done that, as they say :)<\/p>\n\n

To fix this you need to override the Standard factory generation by specifying a name attribute :<\/p>\n

(<\/span>1<\/span>.<\/span>.2<\/span>)<\/span>.<\/span>each<\/span> <\/span>do<\/span> <\/span>|<\/span>i<\/span>|<\/span>\n  <\/span>create<\/span>(<\/span>:<\/span>standard<\/span>,<\/span> <\/span>name:<\/span> <\/span>&<\/span>quot<\/span>;<\/span>1.1<\/span>.<\/span>#<\/span>{<\/span>i<\/span>}<\/span>&<\/span>quot<\/span>;)<\/span>\nend<\/span>\n<\/pre><\/div>\n

Do you need a pull request for this or are you going to add the changes yourself?<\/p>\n

This spec fails because of how FactoryGirl generates a name sequence for your Standard factory. When you run the spec manually the counter for the sequence starts at 1 but it is not reset when you run rspec through guard. It is a common issue and it's caused by our blind trust of a determinate state of the test database in specs. Been there, done that, as they say :) To fix this you need to override the Standard factory generation by specifying a name attribute : (1..2).each do |i| create(:standard, name: "1.1.#{i}") end Do you need a pull request for this or are you going to add the changes yourself?? **UPDATE1** If you're wondering what's the use of the FactoryGirl `sequence` method, I generally use it to ensure uniqueness validations. For example : #app/models/user.rb validates :email, presence: true, uniqueness: true #factories.rb factory :user do sequence(:username) { |n| "user#{n}"} email { |user| "#{user.username}@example.com".downcase} password "password" password_confirmation { |user| user.password} end #user_spec.rb describe User do it "requires a unique email" do existing_user = create(:user) User.new.should have(2).error_on(:email) User.new(email: existing_user.email).should have(2).error_on(:email) end end

User: kc00l

Question: Rspec tests fail in Guard but manually running rspec test passes. Why?

Back to question