DenicioCode
DenicioCode

Reputation: 9336

Weird behaviour in Rspec

I am running some test in my controller spec and I found a weird behaviour I cannot explain why this is happen.

My Spec look like this:

require 'rails_helper'

describe BooksController do
  let(:user_with_books) { create :user, :with_books }

  ...

  describe 'GET /books/:book_id/owners' do
    it 'shows all owners of the book' do
      book = user_with_books.books.first
      user_2 = create :user
      book.owners << user_2
      get :owners, book_id: user_with_books.books.first.id
      expect(assigns(:users).count).to eq 2
      expect(assigns(:users).first).to eq user_with_books
      expect(assigns(:users).second).to eq user_2
    end
  end
end

If I run the command rspec spec/controllers/books_controller_spec.rb:31 everything is green: enter image description here

But if I run just rspec, the test will fail!

enter image description here

What is Rspec doing on this spec to change the behaviour? What can I do to fix this issue?

EDIT: My spec_helper.rb

RSpec.configure do |config|
  config.include FactoryGirl::Syntax::Methods
  config.infer_spec_type_from_file_location!
  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end
end

Upvotes: 0

Views: 99

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

According to your spec_helper.rb, you are never calling DatabaseCleaner.clean. It should be called to actually clean the database up in, e. g. before(:each) filter:

config.before(:each) do
  DatabaseCleaner.clean
end

Upvotes: 1

Related Questions