Search code examples
ruby-on-railsrspectravis-cirspec-rails

Travis CI RSpec tests. Comparing variables that hold the same time


I have such test

  it 'could not update question' do
    old_title = question.title
    old_body = question.body
    old_updated_at = question.updated_at

    patch :update, id: question, question: attributes_for(:question, title: 'new valid title', body: 'new valid body'), format: :js
    question.reload

    expect(question.title).to eq old_title
    expect(question.body).to eq old_body
    expect(question.updated_at).to eq old_updated_at
  end

It raises an error:

1) QuestionsController PATCH #update question by someone else could not update question Failure/Error: expect(question.updated_at).to eq old_updated_at

   expected: 2016-04-09 18:05:03.650576201 +0000
        got: 2016-04-09 18:05:03.650576000 +0000

How could it be different?? On my local machine it passes


Solution

  • You might be able to improve the structure of your test. Consider using mocks & stubs rather than testing state mutation (or lack thereof):

    it 'could not update question' do
        expect(Question).to_not receive(:update)  # Depending on how your model gets updated
        patch :update, id: question, question: attributes_for(:question, title: 'new valid title', body: 'new valid body'), format: :js
    end