以下Capybara / RSpec测试在我的Rails应用程序上失败,我不知道为什么。我正在使用simple_form_for Gem创建表单并提交按钮。就像我更改时一样,更新方法似乎正常工作
expect(@coin.currency_name).to eq('Updated Name')
到
expect(page).to have_text('Updated Name')
测试通过,更新的名称显示在新页面上。但是,当我使用上述的Expect方法时, @ coin.currency_name 似乎没有更新。当我手动更新硬币模型(在页面上,未使用RSpec)时,它可以正常工作,并且 currency_name 已更新。
在此测试中我做错了什么?
规格/功能/硬币/ 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>
和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>
答案 0 :(得分:3)
更改型号后,请使用reload
方法:
click_button 'Submit'
@coin.reload
expect(@coin.currency_name).to eq('Updated Name')