什么时候我应该在Cucumber& RSpec工作流程? [英] When should I test Views separately in Cucumber & RSpec workflow?

查看:88
本文介绍了什么时候我应该在Cucumber& RSpec工作流程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过一段时间的黄瓜& RSpec BDD,我意识到我的许多Cucumber功能只是更高级别的视图测试。



当我开始写我的方案,然后下降到RSpec,我不曾经写查看规格,因为我可以复制和粘贴的情况下,这将是丑陋dupliacation的一部分。



就拿这个场景例如:

 场景:新用户来到网站
给定我没有登录
当我去主页
然后我应该看到免费注册

我知道这不是直接测试视图,但是编写单独的视图规范来检查同样的事情对我来说似乎是多余的。



我接近Cucumber错了吗? 我应该在视图规格中测试什么?



我应该为每个视图编写它们,例如测试视图, >

  def show 
@project = current_user.projects.first
end

解决方案

>



这是一个被广泛接受(在我看来,不正确)黄瓜哲学,视图应该从不在RSpec内测试。参数是,因为视图的行为可以在Cucumber中描述,RSpec应该坚持它最知道的 - 模型和控制器。



我认为,Cucumber的人类可读方面使得查看视图的一些方面非常重要。例如,我发现视图规范在与前端开发人员并行工作时工作得很好。如果JavaScript开发人员知道他想要在您的网页上加入选择器,请务必让您的视图提供该选择器。



例如:

  describe'gremlins / show.html。给了它在午夜之后做
它HAML'做
上下文有#gremlin_warning选择做
Time.stub!(:现在).and_return(Time.parse2010- 12-16 00:01:00')
rendered.should have_selector'#gremlin_warning'



语境这是在午夜之前做
它不具有#gremlin_warning选择做
Time.stub!(:现在).and_return(Time.parse2010-12-16 23:59:00)
呈现。 should_not have_selector'#gremlin_warning'
end
end
end

请注意,规范不描述内容,他们故意简要,他们不描述交互行为。因为视图是你的应用程序中将改变最多的部分,所以应该谨慎使用视图规范。



tl; dr :查看规格用于将合同告知其他开发人员,应谨慎使用(但强>使用)。


After some time of doing Cucumber & RSpec BDD, I realized that many of my Cucumber features are just higher level view tests.

When I start writing my scenario and then go down to RSpec, I don't ever write view specs, since I could just copy and paste part of the scenario, which would be ugly dupliacation.

Take this scenario for example

Scenario: New user comes to the site
  Given I am not signed in
  When I go to the home page
  Then I should see "Sign up free"

I know that this isn't directly testing the view, but writing separate view spec to check for the same thing seems redundant to me.

Am I approaching Cucumber wrong? What exactly should I test in view specs?

Should I write them for every single view, for example testing views for actions like

def show
  @project = current_user.projects.first
end

or should I just test more complex views?

解决方案

It's a widely-accepted (and in my opinion, incorrect) Cucumber philosophy that views should never be tested within RSpec. The argument goes that since the behavior of the view can be described in Cucumber, RSpec should stick to what it knows best -- Models and Controllers.

I argue that the "human-readable" aspect of Cucumber makes some aspects of view-speccing important. For instance, I find view specs to work very well when working in parallel with a front-end developer. If a JavaScript developer knows that he'll want to hook into a selector on your page, it's important that your view provides that selector.

For example:

describe 'gremlins/show.html.haml' do
  context 'given it is after midnight' do
    it 'has a #gremlin_warning selector' do
      Time.stub!(:now).and_return(Time.parse '2010-12-16 00:01:00')
      rendered.should have_selector '#gremlin_warning'
    end
  end

  context 'it is before midnight' do
    it 'does not have a #gremlin_warning selector' do
      Time.stub!(:now).and_return(Time.parse '2010-12-16 23:59:00')
      rendered.should_not have_selector '#gremlin_warning'
    end
  end
end

Note that the specs do not describe the content, they are willfully brief, and they do not describe interaction behaviors. Because the view is the portion of your application that will change the most, view specs should be used sparingly.

tl;dr: View specs are for communicating a contract to other developers and should be used sparingly (but nonetheless should be used).

这篇关于什么时候我应该在Cucumber& RSpec工作流程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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