Im trying to grasp some concepts of TDD and in my RoR app I have /about view which belongs to static_pages#about. It has route defined in routes.rb get 'about' => 'static_pages#about'
. Everything works so far in browser but I want to test it by RSpec as well.
Given
RSpec.describe "about.html.erb", type: :view do
it "renders about view" do
render :template => "about"
expect(response).to render_template('/about')
end
end
raises an error
Missing template /about with {:locale=>[:en], :formats=>[:html, :text, :js, :css, :ics, :csv, :vcf, :png, :......
Thanks!
This spec makes very little sense - the whole idea of a view spec is that you render the view under test and then write expectations (assertions in TDD-speak) about its content. View specs are sometimes useful for testing complex views but are not what you need in this case.
If you want to test that the controller renders the correct template you would do it in a controller spec instead.
require 'rails_helper'
RSpec.describe StaticPagesController, type: :controller do
describe "GET /about" do
it "renders the correct template" do
get :about
expect(response).to render_template "static_pages/about"
end
end
end
Although this kind of spec usually has little value - you're just testing the default behavior of rails and this can be covered by a feature spec which adds more value:
require 'rails_helper'
RSpec.feature "About page" do
before do
visit root_path
end
scenario "as a vistior I should be able to visit the about page" do
click_link "About"
expect(page).to have_content "About AcmeCorp"
end
end
Note that here we have left the world of TDD an stepped into what is called Behavior Driven Development (BDD). Which is more concerned with the behavior of software and less with the nitty gritty details of how it gets the job done.