Ruby-on-rails RSpec 和 Cucumber 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11762245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between RSpec and Cucumber?
提问by banditKing
I have 6 months of Rails development experience. I've built a web application that's in use now with authentication and authorization and postgresql db.
我有 6 个月的 Rails 开发经验。我已经构建了一个 Web 应用程序,现在正在使用身份验证和授权以及 postgresql 数据库。
I'm moving on to my second Rails application but this time, after lessons learnt, I would like to develop it using TDD, since I noticed its a lot easier to scale it and fix bugs. It's slow to develop but in the long run its much easier to deal with.
我将继续我的第二个 Rails 应用程序,但这一次,在吸取教训之后,我想使用 TDD 开发它,因为我注意到它更容易扩展和修复错误。它的开发速度很慢,但从长远来看,它更容易处理。
I have heard of Rspec and Cucumber but am thoroughly confused by them.
我听说过 Rspec 和 Cucumber,但对它们感到非常困惑。
I would like to know what the difference is between RSpec and Cucumber and what they are used for.
我想知道 RSpec 和 Cucumber 之间有什么区别以及它们的用途。
It would also be useful to know if, from the perspective of a beginner (who is also the sole developer) whether a testing framework is really needed.
从初学者(也是唯一的开发人员)的角度了解是否真的需要测试框架也很有用。
回答by DVG
RSpec and Cucumber are both testing frameworks. RSpec includes traditional Unit Testing (which means testing a class or part of the application in isolation from the rest of the application. So your model does what your model is supposed to do, the controller does what it is supposed to do, etc).
RSpec 和 Cucumber 都是测试框架。RSpec 包括传统的单元测试(这意味着测试一个类或应用程序的一部分,与应用程序的其余部分隔离。所以你的模型做你的模型应该做的事情,控制器做它应该做的事情,等等)。
RSpec and Cucumber both are used for Acceptance Testing (Which is called ATDD, BDD, Specification by Example, etc depending on who you ask). These are business-case driven Integration Tests, which mean they simulate the way a user uses the application and uses the full Rails stack so problems with the way the different parts of your application work together can be found in a way that unit testing will not find.
RSpec 和 Cucumber 都用于验收测试(根据您的要求,称为 ATDD、BDD、示例规范等)。这些是业务案例驱动的集成测试,这意味着它们模拟用户使用应用程序的方式并使用完整的 Rails 堆栈,因此可以通过单元测试不会发现的方式发现应用程序不同部分协同工作的方式问题找。
The main difference between RSpec and Cucumber are the business readability factor. Cucumber's main draw is that the specification (features) are separate from the test code, so your product owners can provide or review the specification without having to dig through code. These are the .feature files that you make in Cucumber. RSpec has a similar mechanism, but instead you describe a step with a Describe, Context or It block that contains the business specification, and then immediately have the code that executes that statement. This approach is a little easier for developers to work with but a little harder for non-technical folks.
RSpec 和 Cucumber 之间的主要区别在于业务可读性因素。Cucumber 的主要吸引力在于规范(功能)与测试代码是分开的,因此您的产品所有者可以提供或规范,而无需深入研究代码。这些是您在 Cucumber 中制作的 .feature 文件。RSpec 具有类似的机制,但您可以使用包含业务规范的 Describe、Context 或 It 块来描述步骤,然后立即拥有执行该语句的代码。这种方法对于开发人员来说更容易使用,但对于非技术人员来说有点困难。
Which to use? If you are the sole developer and product owner, then I would stick with RSpec, I feel it's easier to a technical person to understand, offers a few advantages in keeping things scoped and under control, and keep you out of messing with RegExs for test steps. If you are building this for a client, and they are hands-on with regard to the Specification, go with Cucumber for your Acceptance Test and use RSpec for Unit Tests.
使用哪个?如果您是唯一的开发人员和产品所有者,那么我会坚持使用 RSpec,我觉得它更容易让技术人员理解,在保持范围和控制方面提供了一些优势,并使您免于使用 RegEx 进行测试脚步。如果您正在为客户构建它,并且他们在规范方面亲自动手,请使用 Cucumber 进行验收测试并使用 RSpec 进行单元测试。
Just to demonstrate the main difference between the two:
只是为了证明两者之间的主要区别:
Cucumber:
黄瓜:
#articles.feature
Given an article exists called "Testing Demonstration"
When I visit the list of articles
Then I should see an article called "Testing Demonstration"
#article_steps.rb
Given /^an article exists called "(.+)"$/ do |title|
FactoryGirl.create(:article, title: title)
end
When /^I visit the list of articles$/ do
visit articles_path
end
Then /^I should see an article called "(.+)"$/ do |title|
page.should have_content title
end
Rspec
规格
describe "Articles" do
let(:article) { FactoryGirl.create(:article) }
context "Index Page" do
before { visit articles_path }
it { page.should have_content article.title }
end
end
This blogseries is excellent on getting going with RSpec.
这个博客系列非常适合开始使用 RSpec。

