简单表单关联的Rspec测试



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

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

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

是否有办法为选择框生成集合,以便Capybara可以拾取它?

我使用Rails 3.1, Simple Form, Capybara和FactoryGirl。

My code is…

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

我强烈建议为您的测试创建fixture。

这样您就可以手动创建和操作测试所需的记录。它不像使用mock、stub和double那样高效或优雅,但它加强了对应用程序和测试的理解。

最新更新