Ruby 使用动态计算参数调用共享示例
我是capybara、rspec集成测试的新手。 如何使用动态计算的参数调用共享示例Ruby 使用动态计算参数调用共享示例,ruby,rspec,capybara,integration-testing,Ruby,Rspec,Capybara,Integration Testing,我是capybara、rspec集成测试的新手。 如何使用动态计算的参数调用共享示例 shared_examples_for "a measurable object" do |example, display_name| it "is example - #{display_name}" do visit "www.example.com?args=test" expect(page.find("#examplediv").text).to eq example
shared_examples_for "a measurable object" do |example, display_name|
it "is example - #{display_name}" do
visit "www.example.com?args=test"
expect(page.find("#examplediv").text).to eq example
end
end
describe "example" do
# where to compute this dynamic_value
it_behaves_like "a measurable object", dynamic_value, "example 1"
end
描述和共享示例都在单独的文件中
在上面的代码片段中,我希望根据从方法调用获得的数据计算动态_值
我在哪里计算“动态_值”的值
我在before:each
和before:all
中尝试过计算,但这两种方法都不起作用
如果你能用descripe向我解释一下通话周期,那也太好了
提前谢谢。好的,我仍然不能100%肯定我理解你的意图,但我想我现在有足够的时间来提供一个基本的解释。我将这样实施这一概念:
shared_examples_for "a page parser" do |dom_object,value|
it "the text in #{dom_object} should equal #{value} on #{url}" do
visit url
expect(page.find("##{dom_object}").text).to eq value
end
end
describe "example" do
let(:url) { "www.example.com?args=test" }
values_obtained_from_service_call = Service.call(url)
# We will assume this is something like [{dom_object: examplediv, value: "Hello World!"}]
values_obtained_from_service_call.each do |test|
it_should_behave_like "a page parser", test[:dom_object], test[:value]
end
end
这将遍历从服务调用中获得的值,并使用共享示例对它们进行测试
正如我所说,我仍然不确定您为什么要这样做,但从功能上来说,它应该会起作用。好的,我仍然不能100%确定我理解您的意图,但我想我现在有足够的理由提供一个基本的解释。我将这样实施这一概念:
shared_examples_for "a page parser" do |dom_object,value|
it "the text in #{dom_object} should equal #{value} on #{url}" do
visit url
expect(page.find("##{dom_object}").text).to eq value
end
end
describe "example" do
let(:url) { "www.example.com?args=test" }
values_obtained_from_service_call = Service.call(url)
# We will assume this is something like [{dom_object: examplediv, value: "Hello World!"}]
values_obtained_from_service_call.each do |test|
it_should_behave_like "a page parser", test[:dom_object], test[:value]
end
end
这将遍历从服务调用中获得的值,并使用共享示例对它们进行测试
正如我所说,我仍然不确定您为什么要这样做,但从功能上来说,它应该可以工作。这至少可以帮助您完成“通话周期”。除此之外,你的问题对我来说有点不清楚,因为Metat博客没有讨论共享的示例,当it行为类似时,会进行预处理以创建独特的“it”基于参数的块。您询问了descripe块的调用周期,并提到尝试使用
before:each
和before:all
,这就是我的评论。否则,您的问题不清楚您的愿望是什么,因为根据所问问题传递的唯一值是字符串“dynamic value”(expect(示例)。对于eq“dynamic value”
),很抱歉我的问题造成了混乱。我试图用伪代码描述我的问题。我已经更新了我的代码。我不明白在哪里计算动态值变量,因为它的行为像。我感谢你的帮助before:each
和before:all
对我没有帮助。好吧,你关心的是它的行为像。你是说你想随机测试不同的共享组,因为it\u行为类似于的语法是it\u行为类似于“一个可测量的对象”,值,“示例1”
,但当你专门测试页面上的内容时,动态值似乎很奇怪。这至少对你的“通话周期”有所帮助。除此之外,你的问题对我来说有点不清楚,因为Metat博客没有讨论共享的示例,当it行为类似时,会进行预处理以创建独特的“it”基于参数的块。您询问了descripe块的调用周期,并提到尝试使用before:each
和before:all
,这就是我的评论。否则,您的问题不清楚您的愿望是什么,因为根据所问问题传递的唯一值是字符串“dynamic value”(expect(示例)。对于eq“dynamic value”
),很抱歉我的问题造成了混乱。我试图用伪代码描述我的问题。我已经更新了我的代码。我不明白在哪里计算动态值变量,因为它的行为像。我感谢你的帮助before:each
和before:all
对我没有帮助。好吧,你关心的是它的行为像。你是说你想随机测试不同的共享组,因为it\u行为类似于的语法是it\u行为类似于“一个可测量的对象”,值,“示例1”
,但当你专门测试页面上的内容时,动态值似乎很奇怪。