Rspec tests fail in Guard but manually running rspec test passes. Why?
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Hi Bountify.

I have a test case that seems to fail in a peculiar way and I’m not sure why.

When in Guard, I get this error.

However, when I manually run rspec ./spec/features/manage_standards_spec.rb, the test passes.

I can’t seem to get guard to pass the exact same spec. How would I fix that?

Here’s the branch with current code.

https://github.com/akshatpradhan/compliance_chimp/compare/show_standards_for_users

Here's some simple instructions to provision the app with mongodb and github authentication here: https://github.com/akshatpradhan/compliance_chimp/wiki/provisioniong

PS

I wanted to take this opportunity to thank the friends I made here and the help I received from the community. I wanted to post bounties earlier, but I felt so embarrassed from disappearing from the community past couple of months. I don’t know if it was of any help, but I keep talking about you guys at UX Boston and the Boston Security Meetup.

What happened to Lending round?

For those interested, I also wanted to update you on what happened with Lending round (a simple way to manage a small loan to your friend). After talking with about 30 people, I realized most people liked the idea but nobody said they “needed” it.

In fact, the most interesting feedback I got was, “This is a very small vertical. I can only think of a few people I know that would really find use for this, but the prototype is really well done and I hope it works well.” His feedback, I think, was very correct. This is such a small vertical and I couldn’t find anybody who was aching for it. In fact, LendFriend.com has a fully functional application and I think they’re actually struggling. I do think they are making a mistake going with PayPal and should switch to Dwolla to reduce the transaction fees so that they can automate the process of withdrawing from the borrower’s account and depositing it into the lender’s account. It’s unfortunate that Dwolla is US only though.

Where will I go with Lending round?

It’ll remain MIT licensed on github. I’ll probably add some styles to the site to make it look pretty so that it can be referenced as an accomplished project. A very big big big huge huge personal thank you to @alixaxel and @kc00l and please feel free to add it to your own portfolio if you think it worthy. I think hiring managers like this kind of collaboration in a portfolio.

What are you doing next?

I pivoted into an area that is more of my expertise. I spearheaded PCI compliance for a nonprofit and am really aware of the struggles that are involved. I want to make that job easier, hence, Compliance chimp - a simple way to manage PCI Compliance. I think I’ve come a long way as a Rails Developer. I’ve learned so many interesting development/design practices and I’m applying that to Compliance chimp. In fact, I’ve already written my proudest commit (https://github.com/akshatpradhan/compliance_chimp/pull/1/files). To advanced developers it might not look like much, but this is the first time I’ve been able to add a feature, with a test case, and ONLY implement the features of that test case. Its my cleanest commit ever and I understand every bit of that code and how it works together.

Hi akshat, glad you found your way back to Ruby and thanks for all your kind words! That said, time for a bit of git clone :)
kc00l over 5 years ago
Ah kc00l! I'm here at the console ready to debug! I've done a rake db:drop && rake db:setup && rake db:test:prepare with no luck
akshatpradhan over 5 years ago
specs pass on my clone either by running rspec manually or within guard. Time for reverse debug :)
kc00l over 5 years ago
Did you commit all changes?
kc00l over 5 years ago
yeah, the commit with "Why is guard failing" is the same commit i'm working on as you. If I did something rails console RAILS_ENV=test would it help with trying to figure out what's in the test database?
akshatpradhan over 5 years ago
Something weird is happening in your test database, let me redo the whole thing again. Yeah, can you do a Standard.all in your test console ?
kc00l over 5 years ago
Would you be able to gist the commands you run during the provisioning process? I can run your commands and see if it helps.
akshatpradhan over 5 years ago
sure I'll post them in the solution as a work in progress
kc00l over 5 years ago
rake db:test:prepare doesn't actually do anything, blah =/ http://mongoid.org/en/mongoid/docs/rails.html#rake
akshatpradhan over 5 years ago
@kc00l hey, press enter a couple of times at the guard prompt. Maybe you'll get the error a 2nd or 3rd time the specs run.
akshatpradhan over 5 years ago
interesting, now its doing the same error on view_homepage_spec.rb I'm still on the same commit, and haven't added or subtracted any code. In the following gist, I exit guard, purge the db, setup the db, start guard up, press enter to run all the specs. and here's the gist showing the same error on viewhomepagespec.rb instead of managestandardsspec.rb https://gist.github.com/akshatpradhan/9333416
akshatpradhan over 5 years ago
I found out the issue, I'm posting the solution now.
kc00l over 5 years ago
awarded to kc00l

Crowdsource coding tasks.

1 Solution

Winning solution

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
Let me try this out!
akshatpradhan over 5 years ago
The same changes in your other capybara spec make it pass each time you run the spec through guard
kc00l over 5 years ago
This solves it! I wonder what's the point of even having sequence(:name) do |n| "1.1.#{n}" in my standards factory.
akshatpradhan over 5 years ago
I'll add something about this in the solution for the record.
kc00l over 5 years ago
@kc00l Huge thank you, so glad to see you're still here! :)
akshatpradhan over 5 years ago