Reputation: 10541
Firstly what is the meaning behind the render_template
method? What is a template? Is it a view? In which case, in a controller spec, does
response.should render_template('show')
Mean a request to this controller should render a view named show.html.erb
? Or that the controller should contain this
render 'show'
So if I'm right in thinking this, which is the best way to test that view has been rendered?
Option 1:
it "should render a view" do
response.should contain("I am the show view") # assuming the text is present in the view
end
Option 2:
it "should render a view" do
response.should render_template('show')
end
Does Option 2 only pass if you explicitly have render 'show'
in your controller?
def create
if params[:friend_id]
@friend = User.find_by_profile_name(params[:friend_id])
else
end
if @friend.profile_name != nil
@user_friendship = current_user.user_friendships.new(friend: @friend)
@user_friendship.save
redirect_to profile_path(@friend.profile_name), status: 302
else
flash[:error] = "Friend required!"
redirect_to root_path, status: 302
end
end
When testing the create
action with a valid request, the profile_path
does indeed lead to the show view, but is this out of the scope of the spec? I get this error you see:
Failure/Error: response.should render_template('show')
expecting <"show"> but rendering with <[]>
Or another way?
Also, in order to use methods such as contain
in an rspec test, does a headless server such as capybara-webkit
, or a headed sever such as the selinium-webdriver
need to be installed?
Upvotes: 0
Views: 1108
Reputation: 383
You should not want to test this in one test. You will find yourself 'fixing' tests allover the place when you decide to change the flow of the application.
Use separate tests:
Example:
describe "POST #create" do
it "should redirect to profile_path" do
post 'create', valid_params
response.should redirect_to profile_path
end
end
describe "GET #show" do
it "should render show template" do
get 'show', valid_params
response.should render_template('show')
end
end
Upvotes: 1