2011-01-24 61 views
1

我们有一些配置问题,我们想通过在Cucumber中添加“特殊常量”来解决。例如,我们希望在一个步骤中使用文本"__USER__"的任何地方,应该用运行该应用程序的当前用户替换(以便我们可以测试用户权限等内容)。在匹配之前修改黄瓜中的步骤文本

我试图采取朝着这个战略是做这样的事情:

BeforeStep do |step| 
    domain = get_domain() 
    username = get_username() 
    step.text.gsub("__USER__", "#{domain}/#{username}") 
end 

然而,没有BeforeStep。我试图使用Before do |scenario| ... end,但该场景没有任何我可以使用的。我们如何在代码中将"__USER__"的实例替换为用户(以及"__CURRENT_DATE__"与当前日期的实例等)?

回答

0

我通常使用黄瓜通过视图登录用户,然后从那里运行测试。

喜欢的东西:

Given a user exists with email: "[email protected]", account_type: "customer" 
When I go to the homepage 
And I follow "Sign in" 
And I fill in "email" with "[email protected]" 
And I fill in "password" with "password" 
And I press "Sign in" 
0

我认为这是对写作更声明步骤,如When the user logs in相对于这样的事情When I fill in "txt_user_name" with "fred"的情况。这将是很容易写你的步骤定义在这种情况下:

When /^the user logs in$/ do 
    domain = get_domain() 
    username = get_username() 
    fill_in "txt_user_name", :with => "#{domain}/#{username}" 
end 

你甚至可以把在步骤参数变换把文本“用户”到你所需要的用户名,所以你不必反复进行更改:

CAPTURE_USER = Transform /^(the user)$/ do |this_isnt_used| 
    domain = get_domain() 
    username = get_username() 
    "#{domain}/#{username}" 
end 

When /^(#{CAPTURE_USER }) logs in$/ do |user_name| 
    puts "Logging in as #{user_name}" 
end 

这将匹配步骤Given the user logs in,并通过正确的用户名作为参数。

另外,回顾一下你的问题,你可以使用一个转换来完成你正在尝试做的事情,并且它可以代替__USER__的实例,但是我不会自己选择这个选项 - 感觉它会影响该场景的可读性太大。尽管你的选择!