使用 Cucumber 测试 Ruby on Rails 嵌套视图
我刚刚开始使用 Cucumber,不确定如何测试视图中的部分内容。
这是我的特色
黄瓜:posting.feature
Scenario: After having created job posting
Given I am authenticated as "administrator"
Given I am on the new posting page
When I press "Create Posting"
Then I should see "Back to list of all postings"
(这是我当前所在的页面)
我的new.html.haml
= link_to 'Logout', logged_out_url(@user)
%h1 New posting
= render 'form'
%br/
= link_to 'Cancel', postings_path
(这是在 new.html.haml 中加载的视图,我想测试其“创建发布”按钮)
_form.html.haml
= form_for(@postings) do |posting_builder|
- if @postings.errors.any?
#error_explanation
%h2
= pluralize(@postings.errors.count, "error")
prohibited this posting from being saved:
%ul
- @postings.errors.full_messages.each do |msg|
%li= msg
.field
%b= posting_builder.label :title
%br/
= posting_builder.text_field :title
.field
%b= posting_builder.label :description
%br/
= posting_builder.text_field :description
.field
%b= posting_builder.label :requirements
%br/
= posting_builder.text_field :requirements
.field
%b= posting_builder.label :location
%br/
= posting_builder.text_field :location
.field
%b= posting_builder.label :Admin
%br/
= select("posting", "user_id", User.order('last_name ASC').collect {|u| [u.fullname, u.id]})
%br/
%br/
/ Submit
.actions
= posting_builder.submit
对于我应该如何解决这个问题,是否有任何建议或与我的问题相关的资源/示例?
I just started using Cucumber and am not sure how to test a partial I have within a view.
Here's my feature
cucumber: posting.feature
Scenario: After having created job posting
Given I am authenticated as "administrator"
Given I am on the new posting page
When I press "Create Posting"
Then I should see "Back to list of all postings"
(This is the page I'm currently on)
My new.html.haml
= link_to 'Logout', logged_out_url(@user)
%h1 New posting
= render 'form'
%br/
= link_to 'Cancel', postings_path
(This is the view that is loaded in new.html.haml whose 'create posting' button I'd like to test)
_form.html.haml
= form_for(@postings) do |posting_builder|
- if @postings.errors.any?
#error_explanation
%h2
= pluralize(@postings.errors.count, "error")
prohibited this posting from being saved:
%ul
- @postings.errors.full_messages.each do |msg|
%li= msg
.field
%b= posting_builder.label :title
%br/
= posting_builder.text_field :title
.field
%b= posting_builder.label :description
%br/
= posting_builder.text_field :description
.field
%b= posting_builder.label :requirements
%br/
= posting_builder.text_field :requirements
.field
%b= posting_builder.label :location
%br/
= posting_builder.text_field :location
.field
%b= posting_builder.label :Admin
%br/
= select("posting", "user_id", User.order('last_name ASC').collect {|u| [u.fullname, u.id]})
%br/
%br/
/ Submit
.actions
= posting_builder.submit
Are there any suggestions as to how I should tackle this or resources/examples that would be relevant to my question?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不必尝试单独测试部分。 Cucumber 不会看到与主页分开的部分内容,它会看到完全渲染的页面(包含所有部分内容)。因此,如果您这样做
并且我应该看到[部分文本]
,那么就可以了。如果这不起作用,而您认为它应该起作用,则在
And I should see...
步骤之前添加一个步骤,其中显示And show me the page
,然后它将打开您的默认浏览器并向您显示 Cucumber 所看到的内容。这可能会帮助您了解您实际所在的页面,以及它是否与您的期望不符。注意:当您使用
And show me the page
时,您得到的有时是页面的精简版本(无格式,无 .CSS),因此如果没有,请不要担心看起来就像您通过浏览器浏览时通常看到的页面一样。重要的是它包含 HTML、标签、名称、元素 ID 等。You shouldn't have to try to test the partial separately. Cucumber doesn't see partials separately from the main page, it sees the fully rendered page (with all the partial content included). So, if you do
And I should see [the text from the partial]
then you're fine.If that's not working, and you think it should, then right before the
And I should see...
step, add a step that saysAnd show me the page
, and it will open your default browser and show you what Cucumber sees. This might help you figure out what page you're actually on, and if it doesn't match what you expect.NOTE: When you use
And show me the page
, what you get is sometimes a stripped down (no formatting, no .CSS) version of the page, so don't be concerned it if doesn't look at all like the page you would normally see when browsing to it via the browser. What's important is that it contains the HTML, labels, names, IDs of elements, etc.