[英]Best way to do a visit step to a page with a different format (cucumber)
I'm trying to do something like this: 我正在尝试做这样的事情:
Scenario: Reading feeds via json
When I go to the feeds page using json
Then I should see a json file
And I should see 3 feeds in the json file
However I'm not sure how to get the "When I go to the feeds page using json" to result to feeds_path(:format => "json") in the steps. 但是我不确定如何在步骤中获得“当我使用json进入feed页面”时导致到feeds_path(:format =>“json”)。
What is the best way to do this? 做这个的最好方式是什么?
The solution that will work flexibly with any format (or lack of): 可以灵活地使用任何格式(或缺少)的解决方案:
In your paths.rb file: 在你的paths.rb文件中:
def path_to(page_name)
# Split out format if page_name includes ' using '
# Example: When I go to the accounts page using json
page_name, format = page_name.split(' using ')
case page_name
when /the home\s?page/
'/'
when /the new account page/
#pass format
new_account_path(:format => format)
else
begin
page_name =~ /the (.*) page/
path_components = $1.split(/\s+/)
# Also make sure to pass format to the 'guessed' path
self.send(path_components.push('path').join('_').to_sym, :format => format)
rescue Object => e
raise "Can't find mapping from \"#{page_name}\" to a path.\n" +
"Now, go and add a mapping in #{__FILE__}"
end
end
end
end
Try this: 尝试这个:
When %r{^I go to the (\w+) page using (\w+)$} do |page, format|
path = self.send([page, 'path'].join('_').to_sym, :format => format)
visit path
end
When /^I go to the feeds page using json$/ do
visit feeds_path(:format => "json")
end
You might try something like this in your paths.rb file: 你可以在你的paths.rb文件中尝试这样的东西:
def path_to(page_name)
case page_name
when /^the feeds page( using (.*?))?$/
format, uri = $2, '/feeds'
format.blank? ? uri : "#{uri}.#{format}"
end
end
[UPDATED] [更新]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.