Gandalf StormCrow
Gandalf StormCrow

Reputation: 26192

Rspec fails but behaviour works when I test it via controller

This should be very simple to test, but for some reason my test is failing, please consider the following, model bit:

class User < ActiveRecord::Base 

  def active!
    update_attribute(:active, true)
  end

end

controller :

def activate
    user = User.find_by_uuid(params[:id])
    user.active!

    puts "id #{user.id}"
end

test :

describe 'activate user' do
  let(:user) { FactoryGirl.create(:user) }
      before do
        sign_in user
        visit activate_user_path(id: user.uuid)
        puts "id #{user.id}"
      end

      it 'should be true' do
        save_and_open_page
        user.active.should be_true
      end
end

This test fails :

expected: true value
            got: false

But when I do it with browser, the user gets activated without problems. What am I doing wrong? This really looks like a sily test but still doesn't pass, I've spend more than one hour trying out different stuff, none of which worked.

Upvotes: 0

Views: 82

Answers (1)

spickermann
spickermann

Reputation: 106802

The problem is that the spec still holds the User in the user variable that was created via FactoryGirl and does not know that is was changed in the database. Just reload the user and it should work:

it 'should be true' do
  save_and_open_page
  user.reload.active.should be_true
end

Btw. if active is a boolean you can also spec it this way (what reads much nicer):

user.reload.should be_active

Upvotes: 2

Related Questions