何时在BDD周期中从黄瓜切换到rspec以进行登录过程 [英] When to switch from cucumber to rspec in the BDD cycle for a login procedure

查看:181
本文介绍了何时在BDD周期中从黄瓜切换到rspec以进行登录过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然在尝试理解BDD周期中 cucumber rspec 的组合。



我为一个非常简单的登录系统定义了以下方案:

  :登录

为了访问应用程序
作为用户
我要登录

场景:用户成功登录
给定我作为用户存在
当我去登录页面
并且用gabrielhilal填充username
并且用secret填充password
然后我按登录
然后我应该看到欢迎gabrielhilal
我应该重定向到主页


场景:用户输入错误的电子邮件/密码组合
给定我作为用户存在
当我去登录页面
并且用gabrielhilal填充username
我填写passwordwithwrongpassword
然后按登录
然后我应该看到用户名/密码组合无效。
再次看到登录页面

然后,我开始定义 steps 等待适当时机跳入 rspec



我通过模拟用户 FactoryGirl 并定义步骤的第一步:

 给定(/ ^我作为用户存在$ /)do 
@user = FactoryGirl.create(:user)
end

我为会话添加了路由,控制器和操作:

  / ^我去登录页面$ /)do 
访问login_path
结束

我创建了登录表单:

 当(/ ^ I填写([^] *与([^] *)$ /)do |字段,值| 
fill_in字段,与:value
end
当(/ ^我按([^] *)$ /)do | button |
click_on b $ b end

我添加了Flash消息到我的布局:

 当(/ ^我应该看到([^] *)$ /)do | arg | 
page.should have_content(arg)
end

控制器(static_pages)的主页:

 何时(/ ^我应该重定向到主页$ / b $ b访问home_path 
结束

我解决了最后一步:

$当$(/ ^我应该再次看到登录页面$ /)do
访问login_path
结束


  / code> 

我有所有绿色...我没有觉得需要 rspec 。我缺少什么?



我试图理解我应该用rspec测试什么。我不想再次用rspec测试一切了。

解决方案

一个原因,你不需要使用rspec到目前为止,你的代码真的只是使用功能(由rails提供的东西的外观),而不是创建功能。当你使用一个框架时,这会发生很多。



另一个原因是你的黄瓜情景,工作在相当低的抽象层次。他们有很多关于你如何登录的细节。在这个级别的测试与单元测试空间有些重叠,所以它可以理解,对rspec的需求还不明显。



如果你在更高级别



对于登录,我会为快乐路径写下这样的内容。



给定我是用户
当我登录
然后我应该登录

我可能会写一两个悲伤的路径,例如

 给定我是用户
当我使用错误的密码登录
然后我不应该登录

当你在这个抽象级别工作时,你的场景要少得多(运行时间少),而且细节也少得多。现在你可以用单元测试覆盖一些细节。例如,如果我们有一个方法login_error_message,你在你的视图中使用,那么你可能写单元测试来说像

  context'bad password'
login_error_message.should =='bad password'
end

context'bad login'
login_error_message.should =='login not found '
end

虽然你可以编写黄瓜方案来做到这一点, (特别是在测试的运行时成本),使用单元测试。



一般来说,你想使用Cucumber来记录为什么你需要能够登录和你可以登录,但不能指定你如何登录。当然,这是一个艺术,是高度依赖于你的具体情境,并非常开放不同的意见。但是,如果你想有效地使用黄瓜,你需要为你写的每个场景得到大量的爆炸。


I'm still trying to understand the combination of cucumber and rspec in the BDD cycle.

I have defined the following scenarios for a very simple login system:

Feature: Log in

In order to get access to the application
As a user
I want to log in

Scenario: User logs in successfully
  Given I exist as a user
  When I go to the login page
  And I fill in "username" with "gabrielhilal"
  And I fill in "password" with "secret"
  And I press "Login"
  Then I should see "Welcome gabrielhilal"
  And I should be redirected to the home page


Scenario: User enters wrong email/password combination
  Given I exist as a user
  When I go to the login page
  And I fill in "username" with "gabrielhilal"
  And I fill in "password" with "wrongpassword"
  And I press "Login"
  Then I should see "Invalid username/password combination."
  And I should see the login page again

Then, I started defining the steps waiting for the right moment to jump into the rspec.

I passed the first step simulating the user with FactoryGirl and defining the step:

Given(/^I exist as a user$/) do
  @user = FactoryGirl.create(:user)
end

I added the route, controller and action for session:

When(/^I go to the login page$/) do
  visit login_path
end

I created the form for log in:

When(/^I fill in "([^"]*)" with "([^"]*)"$/) do |field, value|
  fill_in field, with: value
end
When(/^I press "([^"]*)"$/) do |button|
  click_on(button)
end

I added the flash message to my layout:

When(/^I should see "([^"]*)"$/) do |arg|
  page.should have_content(arg)
end

I added the route and controller (static_pages) for the home page:

When(/^I should be redirected to the home page$/) do
  visit home_path
end

I solved the last step:

When(/^I should see the login page again$/) do
  visit login_path
end

And I got all green... I didn't feel the need of rspec through these scenarios. What am I missing?

I'm trying to understand what I should test with rspec. I don't feel right to test everything again with rspec....

解决方案

One reason you haven't needed to use rspec so far is that your code really is just 'using' functionality (provided by rails from the looks of things) rather than creating functionality. This happens quite alot when you use a framework.

Another reason is that your cucumber scenarios, are working at quite a low level of abstraction. There is lots of detail in them about 'how' you login. Testing at this level overlaps somewhat with the unit test space, so its understandeable that the need for rspec is not yet apparent.

If you wrote your scenarios at a higher level of abstraction, then you might get to see where rspec comes into play.

For logging in I would write something like this for the happy path

Given I am a user
When I login 
Then I should be logged in

And I'd write maybe one or two sad paths, e.g.

Given I am a user
When I login with a bad password
Then I should not be logged in

When you work at this level of abstraction you have far fewer scenarios (takes less time to run), but also much less detail. Now you could cover some of that detail with a unit test. For example if we had a method login_error_message, that you were using in your view, then you might write unit tests to say things like

context 'bad password'
  login_error_message.should == 'bad password'
end

context 'bad login'
  login_error_message.should == 'login not found'
end

Whilst you could write cucumber scenarios to do this, it would be much cheaper (particularly in runtime cost of tests), to use a unit test instead.

Generally you want to use Cucumber to document 'why' you need to be able to login and that you can login, but not to specify how you login. Of course this is very much an art, is highly dependent on your particular context, and very open to different opinions. But if you want to use cucumber effectively you need to get plenty of bang for each scenario you write.

这篇关于何时在BDD周期中从黄瓜切换到rspec以进行登录过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆