I am very new to RSpec just starting to test few things.
I have a form with "remote: true" and want to write a spec case for that.
.row
.col-md-offset-4.col-md-4.col-sm-offset-3.col-sm-6
= form_for @reading, html: {class: 'form-inline'}, remote: true do |f|
#error_box
.row
.col-md-9
.input-group.full-width
= f.text_field :label, class: 'form-control', placeholder: t('readings.new.lable_placeholder')
%span.input-group-addon mg/dl
.col-md-3
= f.submit t('readings.new.enter'), class: 'btn btn-success full-width'
#reading_panel
= render partial: 'reading_panel', locals: {readings: @readings}
Here is my controller
def create
@reading = current_user.readings.create(reading_params)
end
create.js.erb that re-renders partials.
<% if @reading.valid? %>
$("#reading_panel").html("<%= j render partial: 'reading_panel', locals: {readings: @readings} %>");
$("#reading_label").val('');
<% else %>
$("#error_box").html("<%= j render partial: 'shared/error_messages', locals: {reading: @reading} %>");
<% end %>
spec with following code
scenario "add a new reading" do
visit new_reading_url
within "#new_reading" do
fill_in "reading_label" , with:10
submit_button = page.find("input[type=submit][value='Enter']")
submit_button.click
expect(Reading.count).to eq(1)
end
end
So what i want to ask is how can i test that response is a js response and check if the partials are rendered or not inside the create.js.erb .
Any help will be much appreciated!!
You shouldn't be checking types of responses in feature tests, just check that the changes you expect to happen on screen happen (you also really shouldn't be directly checking counts in the database in most cases). If you have setup capybara, rspec, and database_cleaner in the normally recommended manor (and selected a javascript capable driver - defaults to selenium) then your test would be something like
scenario "add a new reading", js: true do # in recommended config js: true tells the test to use Capybara.javascript_driver
visit new_reading_url
within "#new_reading" do
fill_in "reading_label" , with: '10'
click_button('Enter')
end
expect(page).to have_css('#reading_panel', text: '10') # assuming it just renders the submitted text into the panel
expect(page).to have_field('reading_label', with: '') # verify the input was cleared
end