having a views:
# views/users/index.html.erb
<%= link_to 'Destroy', user_destroy_path(user), remote: :true %>
having a destroy action:
# controllers/users_controller.rb
respond_to :js
def destroy
@user = user.find(params.require(:id))
@user.destroy
end
which renders:
# views/users/destroy.js.erb
$('#some_div').html('Bye Bye' + user.name);
I want to write controller tests test for it
# spec/controllers/users_controller_spec.rb
describe UsersController do
it 'says goodbye' do
delete :destroy, id: user
expect(response.body).to include('Bye Bye #{user.name}')
end
end
but "response.body" is always empty string. Why? How to examine content of js response?
You need to tell Rspec to render views. It doesn't by default.
require "spec_helper"
describe UsersController, type: :controller do
render_views
it 'says goodbye' do
delete :destroy, id: user
expect(response.body).to include("Bye Bye #{user.name}")
end
end
However, seeing as you are relying on JavaScript, you will not be able to find the text as this will just return HTML.
In order to get this working, you need to use an actual browser, so this means a feature spec using something like PhantomJS (see the poltergeist gem for capybara), or moving to Cucumber.
Your test would then assert things from the user's point of view, something like this:
it 'says goodbye', js: true do
visit home_page
click_button 'Log out'
expect(page).to have_content("Bye Bye #{user.name}")
end