Rails 5,simple_form_for,RSpec和Capybara - click_button不适用于我的表单

问题描述 投票:1回答:1

以下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

应用程序/视图/硬币/ 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>
ruby-on-rails rspec capybara
1个回答
3
投票

更改模型后,使用reload方法:

click_button 'Submit'
@coin.reload
expect(@coin.currency_name).to eq('Updated Name')
© www.soinside.com 2019 - 2024. All rights reserved.