Will Taylor
Will Taylor

Reputation: 2304

Test view correctly rendered in Rspec

How do I do the following in RSpec?

test "should get home" do
  get :home
  assert_response :success
  get :home, { mobile: 1 }
  assert_response :success
end

Note that my mobile views have a different mime-type (i.e. .mobile.erb )

Failed attempt:

render_views
describe "GET home" do
    it "renders the index view" do
      get :home
      expect(response).to render_template("home")
      get :home, { mobile: 1 }
      expect(response).to render_template("home")
    end
  end

This test doesn't fail if I break the mobile view.

Upvotes: 0

Views: 300

Answers (2)

Misha Slyusarev
Misha Slyusarev

Reputation: 1383

To check that the request was successful you can use:

it { is_expected.to respond_with 200 }

As the best practice is to have single expectation per test, I would refactor your example to something like this:

describe "GET home" do
  render_views

  context 'with regular view' do
    before do
      get :home
    end

    it { is_expected.to respond_with 200 }
    it "renders the index view" do
      expect(page).to have_content 'some header'
    end
  end

  context 'with mobile view' do
    before do
      get :home, { mobile: 1 }
    end

     it { is_expected.to respond_with 200 }
     it "renders the index view" do
       expect(page).to have_content 'some header'
     end
  end
end

That's just an idea for you to start.

Upvotes: 1

atw
atw

Reputation: 5830

You can try this:

expect(response).to be_success

Upvotes: 0

Related Questions