2

So I am pretty new to testing with RSPEC

I am just testing the rendering of my controller like so:

describe "GET job" do 
  it "renders the jobs view" do 
  get :jobs
  expect(response).to render_template :jobs
end

However my view (jobs) is a folder, not a template so the above dosnt work. How do i test to see if the response is going to the jobs view folder. inside my jobs view folder i have many different files.

My customer controller action looks like so:

 def jobs
    @jobs = @customer.jobs
 end
2
  • Which template in the folder is the one that should be rendered by the jobs action? Commented Apr 17, 2015 at 4:07
  • render_template("jobs/foobar") Commented Apr 17, 2015 at 4:21

2 Answers 2

4

To test that the template foobar inside app/views/jobs/ is rendered you should use this code:

describe "GET job" do 
  it "renders the jobs view" do 
  get :jobs
  expect(response).to render_template :foobar
end

If you are not testing JobsController and would like to make sure that it still renders app/views/jobs/foobar then you should change it to this line:

expect(response).to render_template("jobs/foobar")
Sign up to request clarification or add additional context in comments.

Comments

2

Ideally, in rspec controller test view rendering has been stubbed by default. To check view rendering in controller specs you have to do as follows:

describe "GET job" do 
  render_views

  it "renders the jobs view" do 
  get :jobs

  expect(response).to render_template("jobs/foobar")
end

where render_views will make render respective views of a controller action.

1 Comment

Does this convert it into a request spec? I did this and it goes from 200ms to 7000ms to run that one test

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.