Reputation: 373
The following Capybara/RSpec test is failing on my Rails app and I can't figure out why. I'm using the simple_form_for Gem to create the form and submit button. The update method seems to be working properly, as when I change
expect(@coin.currency_name).to eq('Updated Name')
to
expect(page).to have_text('Updated Name')
the test passes and the updated name is shown on the new page. However @coin.currency_name doesn't seem to be updated when I use the previously described expect method. When I manually update the Coin model (on the page, not using RSpec) it works fine and the currency_name is updated.
What am I doing wrong on this test?
spec/features/coins/coin_spec
require 'rails_helper'
RSpec.feature 'Coins' do
before(:each) do
@user = FactoryBot.create(:user)
end
context 'update coin' do
scenario 'should succesfully edit name if user=admin' do
@user.update(admin: true)
login_as(@user, :scope => :user)
@coin = Coin.create!(currency_name: "TestName", user_id: @user.id)
visit edit_coin_path(@coin)
fill_in 'Currency Name', with: 'Updated Name'
click_button 'Submit'
expect(@coin.currency_name).to eq('Updated Name')
end
end
end
app/views/coins/edit.html.erb
<div class='form-container'>
<%= simple_form_for @coin, url: coin_path do |f| %>
<h2>Edit Coin</h2>
<div class="form-container__section">
<%= f.input :currency_name, label: "Currency Name", class: 'form-control' %>
<%= f.input :link_name, placeholder: "Link Name", label: false, class: 'form-control' %>
...
<%= f.button :submit, value: "Submit", class: "btn primary-small", style: "margin-top: 20px;" %>
<% end %>
</div>
and the HTML
<div class="form-container">
...
<h2>Edit Coin</h2>
<div class="form-container__section">
<div class="form-group string required coin_currency_name"><label class="control-label string required" for="coin_currency_name"><abbr title="required">*</abbr> Currency Name</label><input class="form-control string required" type="text" value="OldName" name="coin[currency_name]" id="coin_currency_name"></div>
...
<input type="submit" name="commit" value="Submit" class="btn btn-default primary-small" style="margin-top: 20px;" data-disable-with="Update Coin">
</form>
Upvotes: 1
Views: 543
Reputation: 1097
After change of model, use reload
method:
click_button 'Submit'
@coin.reload
expect(@coin.currency_name).to eq('Updated Name')
Upvotes: 3