【问题标题】:Rspec test on Simple Form Associations简单形式关联的 Rspec 测试
【发布时间】:2023-03-13 16:10:02
【问题描述】:

我正在尝试在表单上运行请求规范(使用简单表单构建)。该表单包含一些使用关联方法生成的选择框,因此也包含模型的数据库值。

运行 save_and_open_page 时,它​​看起来不像在下拉菜单中选择值。

我看过 Mocking 和 Stubbing,但这对我来说是新的,我对基本用法之外的概念仍然有些困惑。

有没有什么方法可以为选择框生成集合,以便 Capybara 可以拾取它?

我正在使用 Rails 3.1、Simple Form、Capybara 和 FactoryGirl。

我的代码是……

challenge_spec

describe "New Challenges" do

  before(:all) do
    %w["Under 13", "13 - 16"].each do |item|
      FactoryGirl.create(:age, :name => item)
     end
  end

  it "should redirect to resources after submission" do

    login_valid_user

    visit new_challenge_path

    @challenge = Factory.build(:challenge)

    fill_in "challenge_name", :with => @challenge.name
    fill_in "challenge_description", :with => @challenge.description
    fill_in "challenge_description", :with => @challenge.description
    select "30 mins", :from => "challenge_timescale"
    save_and_open_page
    select 1, :from => "challenge_age_id"
    select @challenge.category, :from => "challenge_category_id"

    click_button "save_button"

  end
end

控制器

def new
  @challenge = Challenge.new

  respond_to do |format|
    format.html # new.html.haml
    format.json { render json: @challenge }
  end
end

表单项

<%= f.association :age, :prompt => "Please select..." %>

模型

挑战

class Challenge < ActiveRecord::Base
  belongs_to :age
end

年龄

class Age < ActiveRecord::Base
  has_many :challenges
end

【问题讨论】:

  • 您选择的字段的标签名称是什么?您是否尝试过使用这些而不是实际的字段名称。例如select "30 mins", :from =&gt; "Challenge Timescale".
  • @nmott 有同样的问题。我正在专门检查每个选项上的标签。当我在浏览器中运行页面时,一切都正确显示。当我在水豚中跑步时,它不会出现在page.html
  • @Drew 需要查看更多详细信息才能提供帮助。您能否创建一个新问题并将链接发回此处,我会看看。

标签: ruby-on-rails rspec capybara simple-form


【解决方案1】:

我强烈建议为您的测试创建固定装置。

通过这种方式,您可以手动创建和操作测试所需的记录。它不像使用模拟、存根和双精度那样高效或优雅,但它加强了对应用程序和测试的理解。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    相关资源
    最近更新 更多