Why Are My Rspec Tests Failing, But My App Is Working

Why are my RSpec tests failing, but my app is working?

I had the same issue and found the answer posted here.

basically RSPEC needs both @current_user and current user to be set for sign_in and sign_out. Weird and annoying, but perhaps the reason one might consider using Devise in a production app!

Rspec tests fail even though everything works fine manually

You need to reload your user object, via user.reload.friend_requests. The in-memory object in your test hasn't been hydrated with any updates.

RSpec tests pass in isolation, but fail when run together

I believe this is an autoload issue. If your ::CodesController gets loaded before the Users::CodesController then your spec for Users::CodesController is actually using ::CodesController and hence can't create the correct routes. When run singly the correct class is autoloaded and everything works correctly. To fix this require the correct class at the beginning of your spec.

# spec/controllers/user/codes_controller_spec.rb

require 'user/codes_controller'
...

and

# spec/controllers/codes_controller_spec.rb

require 'codes_controller'
...

Why do these RSpec tests fail when the app works as expected?

I think I got the solution. Although I am still confused. Here is the code for the tests as they were before the refactoring mentioned in my question (as they are in the tutorial, they all were passing):

describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
let!(:m1) { FactoryGirl.create(:micropost, user: user, content: "Foo") }
let!(:m2) { FactoryGirl.create(:micropost, user: user, content: "Bar") }

before { visit user_path(user) }

it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }

describe "microposts" do
it { should have_content(m1.content) }
it { should have_content(m2.content) }
it { should have_content(user.microposts.count) }
end #some other tests for show page continue, also having the same behaviour
end

While reading the tests to see if there was a mistake I started wondering why those tests were passing if I was not signing the user in, so I added the code to sign in the user in the before block, like this:

describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
let!(:m1) { FactoryGirl.create(:micropost, user: user, content: "Foo") }
let!(:m2) { FactoryGirl.create(:micropost, user: user, content: "Bar") }

before do
valid_signin user
visit user_path(user)
end

it { should have_selector('h1', text: user.name) }
it { should have_selector('title', text: user.name) }

describe "microposts" do
it { should have_content(m1.content) }
it { should have_content(m2.content) }
it { should have_content(user.microposts.count) }
end #some other tests for show page continue, also having the same behaviour
end

And now all tests pass. I know is seems silly and obvious to sign in the user, however that's how the tests are in the tutorial and they were working before. Here is the updated test file if you want to check it. All changes are now committed in my github repo. Thank you all for your help.

Failing (previously passing) RSpec test even after commiting back

It could be that your Test DB is not being cleaned. Try running rails c test and query Product.count to see if the Test DB has the product records which may be causing this error. You can also put a byebug or puts Product.count in your before(:each) block before using the factory to create records to ensure that there ain't no existing records before running the tests. If that's the case, you'll need to clean your DB before and after tests manually or by the DatabaseCleaner Gem.

Tip:
It's also better to use a variable for the number of records you're creating and expecting.
Example:

products_count = 4.times { FactoryGirl.create :product }
expect(products_response).to have(products_count).items

RSPEC test fail, app works

Just add to your factory.rb something like

Factory.sequence :name do |n|
"person-#{n}"
end

Rails Rspec Suite Has Failures But They Pass When Run Individually

I can't explain what is really causing the error. But it must due to setting the Sidekiq test mode globally. Remove the Sidekiq setting from the head section of the specs and try the following:

    before do 
Sidekiq::Testing.inline! do
post :create, email: 'jen@example.com'
end
end
after do
ActionMailer::Base.deliveries.clear
Sidekiq::Worker.clear_all
end

it 'sends the reset email to the users provided email' do
expect(ActionMailer::Base.deliveries.count).to eq(1)
end


Related Topics



Leave a reply



Submit