使用参数调用自定义步骤中的现有步骤
call an existing step inside a custom step with parameter
我正在使用 calabash-android 来测试我的应用程序。
我创建了自己的步骤:
Then /^There should be (\d+) customers$/ do |nr_of_customers|
...
end
在那之后,我创建了另一个步骤,它需要调用上面现有的步骤,我知道我可以使用宏,所以我尝试了这个:
Given /^I hosted (\d+) customers$/ do |nr_of_customers|
#How to pass the nr_of_customers to the macro???
macro 'There should be nr_of_customers'
...
end
但是,如何将参数 nr_of_customers
传递给调用其他步骤函数的宏?
不要在步骤中调用步骤,否则最终会得到一团乱七八糟的代码。而是从您的步骤定义中提取辅助方法并改为调用它们。
例如
Then /^There should be (\d+) customers$/ do |nr_of_customers|
expect(customer_count).to be nr_of_customers
end
Given /^I hosted (\d+) customers$/ do |nr_of_customers|
# do some stuff to set up the customers
expect(customer_count).to be nr_of_customers
...
module StepHelpers
def customer_count
....
此外,在 Givens 中嵌入 then 语句是不好的做法。 Givens 是关于设置状态而不是测试后果所以你的 given 应该是这样的
Given /^I hosted (\d+) customers$/ do |nr_of_customers|
nr_of_customers.times do
host_customer
end
并且host_customer
应该是您在编写显示您可以接待客户的场景时创建的助手。
我正在使用 calabash-android 来测试我的应用程序。
我创建了自己的步骤:
Then /^There should be (\d+) customers$/ do |nr_of_customers|
...
end
在那之后,我创建了另一个步骤,它需要调用上面现有的步骤,我知道我可以使用宏,所以我尝试了这个:
Given /^I hosted (\d+) customers$/ do |nr_of_customers|
#How to pass the nr_of_customers to the macro???
macro 'There should be nr_of_customers'
...
end
但是,如何将参数 nr_of_customers
传递给调用其他步骤函数的宏?
不要在步骤中调用步骤,否则最终会得到一团乱七八糟的代码。而是从您的步骤定义中提取辅助方法并改为调用它们。
例如
Then /^There should be (\d+) customers$/ do |nr_of_customers|
expect(customer_count).to be nr_of_customers
end
Given /^I hosted (\d+) customers$/ do |nr_of_customers|
# do some stuff to set up the customers
expect(customer_count).to be nr_of_customers
...
module StepHelpers
def customer_count
....
此外,在 Givens 中嵌入 then 语句是不好的做法。 Givens 是关于设置状态而不是测试后果所以你的 given 应该是这样的
Given /^I hosted (\d+) customers$/ do |nr_of_customers|
nr_of_customers.times do
host_customer
end
并且host_customer
应该是您在编写显示您可以接待客户的场景时创建的助手。