简单形式关联的 Rspec 测试
我正在尝试在表单上运行请求规范(使用简单表单构建)。该表单包括一些使用关联方法生成的选择框以及模型的数据库值。
运行 save_and_open_page 时,它看起来不像在下拉列表中选择值。
我已经研究过 Mocking 和 Stubbing,但这对我来说是新的,而且我对基本用法之外的概念仍然有点困惑。
有什么方法可以生成选择框的集合,以便水豚可以拾取它?
我正在使用 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
I am trying to run a request spec on a form (built with Simple Form). The form includes some select boxes that are generated using the association method and therefore database values for the model.
When running save_and_open_page it doesn't look like the select the values in the drop downs.
I have looked at Mocking and Stubbing but this is new to me and I'm still a little confused on the concept beyond basic usage.
Is there any way to generate the collection for the select box so Capybara can pick it up?
I'm using Rails 3.1, Simple Form, Capybara and 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
Controller
def new
@challenge = Challenge.new
respond_to do |format|
format.html # new.html.haml
format.json { render json: @challenge }
end
end
Form item
<%= f.association :age, :prompt => "Please select..." %>
Models
Challenge
class Challenge < ActiveRecord::Base
belongs_to :age
end
Age
class Age < ActiveRecord::Base
has_many :challenges
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我强烈建议您为测试创建固定装置。
通过这种方式,您可以手动创建和操作测试所需的记录。它不像使用模拟、存根和双打那样高效或优雅,但它增强了对应用程序和测试的理解。
I strongly recommend creating fixtures for your tests.
This way you can manually create and manipulate the records needed for tests. It's not as efficient or elegant as using mocks, stubs and doubles but it reinforces understanding of the application and tests.