=>context vs describe?
According to the rspec source code, “context” is just a alias method of “describe”, meaning that there is no functional difference between these two methods. However, there is a contextual difference that’ll help to make your tests more understandable by using both of them.
The purpose of “describe” is to wrap a set of tests against one functionality while “context” is to wrap a set of tests against one functionality under the same state. Here’s an example
===================================
=>shared examples?
Making tests is great and you get more confident day after day. But in the end you will start to see code duplication coming up everywhere. Use shared examples to DRY your test suite up.
===================================
=>Expect vs Should syntax ?
On new projects always use the expect syntax
===================================
=>Use subject?
If you have several tests related to the same subject use subject{} to DRY them up.
BAD
it { expect(assigns('message')).to match /it was born in Belville/ }
it { expect(assigns('message').creator).to match /Topolino/ }
GOOD
subject { assigns('message') }
it { should match /it was born in Billville/ }
===================================
=>Automatic tests with guard?
Running all the test suite every time you change your app can be cumbersome. It takes a lot of time and it can break your flow. With Guard you can automate your test suite running only the tests related to the updated spec, model, controller or file you are working at.
GOOD
bundle exec guard
====================================
=>Stubbing HTTP requests?
Sometimes you need to access external services. In these cases you can't rely on the real service but you should stub it with solutions like webmock.
GOOD
context "with unauthorized access" do
let(:uri) { 'http://api.lelylan.com/types' }
before { stub_request(:get, uri).to_return(status: 401, body: fixture('401.json')) }
it "gets a not authorized notification" do
page.driver.get uri
expect(page).to have_content 'Access denied'
end
end
===========================================
=>Useful formatter?
Use a formatter that can give you useful information about the test suite. I personally find fuubar really nice. To make it work add the gem and set fuubar as default formatter in your Guardfile.
GOOD
# Gemfile
group :development, :test do
gem 'fuubar'
===========================================
=>mock vs stub?
Both mock and stub are aliases of the more generic double. Like context and describe, they can be used interchangeably to make the intent of the specs more clear. This is described in a lot more detail in The RSpec Book.
================
cucumber vs rspec?
http://stackoverflow.com/questions/1700970/tdd-bdd-rails-cucumber-rspec-duplication
http://stackoverflow.com/questions/11762245/whats-the-difference-between-rspec-and-cucumber
==============
No comments:
Post a Comment