0

looks like I am losing 'San Francisco' at the start of the Update test in this example - not sure why - when I do a puts response.body at the end the Post Test everything looks ok - here is my flights_spec.rb:

require 'rails_helper'
require 'spec_helper'
require 'flight'

RSpec.describe FlightsController, type: :controller  do

   render_views
   let(:flight0) { Flight.create(:destination => "Las Vegas") }

      it "Get Test" do
        puts 'Get Test'
        get :index, :format => :json
        puts "Body = " + response.body
        expect(response.body).to include('Las Vegas')
     end

     it "Post Test" do
       puts 'Post Test'
       post :create, :flight => {:destination => "San Francisco"}
       puts "Post status:" + response.status.to_s
       expect(response).to redirect_to(assigns(:flight))
       redirect_to(response.headers['Location'])
       get :index, :format => :json
       puts "Body = " + response.body
       expect(response.body).to include("San Francisco")
    end

    it "Update Test" do
        puts 'Delete Test'
        get :index, :format => :json
        puts "Body = " + response.body
        expect(response.body).to include("San Francisco")
    end 
end

1 Answer 1

2

RSpec assumes that you are writing each test to be independent of the other tests. However, the way you have it here, Update Test relies on Post Test to create the data it needs. RSpec automatically undoes any changes you made between each test. You can think of each it block as resetting everything back to the initial state.

A quick fix would be to simply aim the delete at the Las Vegas object instead since this flight is already being recreated for each test because of the let statement:

it "Update Test" do
    puts 'Delete Test'
    get :index, :format => :json
    puts "Body = " + response.body
    expect(response.body).to include("Las Vegas")
    # rest of your test
end 

Also I just would note that you may run into issues with the let statement because it is lazily instantiated, meaning it may not actually create a Las Vegas flight until you actually try to use the flight0 variable in your test. You can fix that by simply adding a bang to the end, like this let!(:flight_0) {}.

2
  • Since there is no need to reference flight_0 anywhere, a before block might be more idiomatic.
    – Andy Waite
    Apr 27, 2015 at 18:40
  • @AndyWaite given the code here I agree with that, but I personally usually just set up with a let or let! statement anyway in case I end up needing to reference it. For example, if the OP decides to use something like Factory_Girl to create Flight objects, then being able to reference said object in the test will be helpful since hard-coding "Las Vegas" won't work anymore; it would need to be something like: expect response.body).to include(flight_0.destination)
    – Rob Wise
    Apr 27, 2015 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.