You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
describe"#add_task"doit'only adds a single task'dotask=Task.new("Feed the cows")list=List.new("Stuff")expect{list.add_task(task)}.tochange{list.tasks.length}.by(1)endend
TDD
Test Driven Development. Write examples before implementation.
BDD
Behaviour-Driven Development is about implementing an application by describing its behavior from the perspective of its stakeholders. (The Rspec Book)
RSpec
(mention alternatives, write a simple hand sewn test)
Testing Terms
Structure of an example (test)
Code Example
An executable example of how the subject code can be used and its expected behavior. Also called a test, calling tests code examples reinforces the documentation value of RSpec tests.
Example Group
defined with the 'describe' method, pass it a string, which is what we'll be describing, this reinforces the idea that Rspec is used to describe and document a system.
It
A block which defines a code example
Test Implementation
Expectations
A requirement for any test. In code examples we set expectations of what should happen. If the expectations aren't met, the test fails.
Mocks/Doubles/Stubs
All the same thing. A test double is an object that stands in for another object in an example (test)
user = mock(:user)
user = double(:user)
user = stub(:user)
Message Stubs
Returning a predefined response to a message within a code example
user.stub(:name).and_return("Bobby McFerrin")
user.stub(:name => "Bobby McGee") -- this is working with an actual User object
user = mock(:user, :name => "Bobby McFerrin") -- this creates a mock user and stub
Chaining Stubs
Sometimes you need to stub deep (watch out for this, it's a bad code smell). stub_chain can help:
user.stub_chain(:account, :billing_address).and_return("717 California St.")
user.account.billing_address # 717 California St.
Expectations
A method stub that will raise an error if it is never called.
Sure. You'll have to circles of Red/Green development. The outer circle, (BDD) and the inner circle (unit testing).
Benefits of TDD
Smaller (shorter) development cycle. Focus on developing and delivering the smallest unit of value (just like your commits!)
Avoid over engineering by writing the simplest code that passes the test when practicing TDD. This discourages you from including rainy day code you might never use.
Develop the interface free of any implementation
Running Thangs
Default rspec output is lame. Make it pretty
rspec --color --format documentation some_spec.rb
Better yet, save this to your ~/.rspec config file:
echo "--color --format documentation" > ~/.rspec
RSpec with Rails
Add rspec-rails to your Gemfile, bundle install then rails generate rspec:install