ChatGPT解决这个技术问题 Extra ChatGPT

What's the difference between RSpec and Cucumber? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 9 years ago.

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.

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.

I have heard of Rspec and Cucumber but am thoroughly confused by them.

I would like to know what the difference is between RSpec and Cucumber and what they are used for.

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.

To confuse you even more, Minitest could also be an option
Capybara is really a driver that can be used in any of the testing frameworks, not a framework in and of itself. Watir-webdriver and Selenium are two things that do a similar functions, but drive a real browser and are much much slower than Capybara driving Rack::Test
How is this not constructive???
Great question and great answer. It's just what I needed. I am baffled by the closing of the question as non-constructive. Who cares if it's going to solicit debate? That's what community voting is for.
Voting to reopen as I think the basis for closing the question are specious. It can definitely be (and has been!) answered using "supported by facts, references, or expertise", and any StackOverflow question possibly "will likely solicit debate, arguments, polling" (which we all know and have seen). As for "polling"? The who modus operandi of SO is about upvoting and downvoting questions and answers. It's basically one big poll. Closing this was daft. It's a good helpful, on topic question and answer.

B
Bulat

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 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.

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.

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.

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 blog series is excellent on getting going with RSpec.


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now