Introduction to RSpec
Sean Cribbs and Joshua French
Kansas City Ruby User Group
Intro to BDD
I had a problem. While using and teaching agile practices like test-driven development (TDD) on projects in different environments, I kept coming across the same confusion and misunderstandings. Programmers wanted to know where to start, what to test and what not to test, how much to test in one go, what to call their tests, and how to understand why a test fails.
Intro to BDD
The deeper I got into TDD, the more I felt that my own journey had been less of a wax-on, wax-off process of gradual mastery than a series of blind alleys. I remember thinking “If only someone had told me that!” far more often than I thought “Wow, a door has opened.” I decided it must be possible to present TDD in a way that gets straight to the good stuff and avoids all the pitfalls.
Intro to BDD
My response is behaviour-driven development (BDD). It has evolved out of established agile practices and is designed to make them more accessible and effective for teams new to agile software delivery. Over time, BDD has grown to encompass the wider picture of agile analysis and automated acceptance testing.
Intro to BDD
- English test method names
Intro to BDD
- English test method names
Intro to BDD
- English test method names
- Use sentences
- Focus tests with "should"
Intro to BDD
- English test method names
- Use sentences
- Focus tests with "should"
- Be expressive - helpful with failures
Intro to BDD
- English test method names
- Use sentences
- Focus tests with "should"
- Be expressive - helpful with failures
- Emphasize behavior, not tests
Intro to BDD
- English test method names
- Use sentences
- Focus tests with "should"
- Be expressive - helpful with failures
- Emphasize behavior, not tests
- Determine the next most important behavior
Intro to BDD
- English test method names
- Use sentences
- Focus tests with "should"
- Be expressive - helpful with failures
- Emphasize behavior, not tests
- Determine the next most important behavior
- Requirements are behavior
Intro to BDD
- English test method names
- Use sentences
- Focus tests with "should"
- Be expressive - helpful with failures
- Emphasize behavior, not tests
- Determine the next most important behavior
- Requirements are behavior
- Acceptance criteria should be executable
Test::Unit vs. RSpec
- test suites
- descriptions
- tests
Test::Unit vs. RSpec
- test suites
- descriptions
- tests
- examples
Test::Unit vs. RSpec
- test suites
- descriptions
- tests
- examples
- assertions
Test::Unit vs. RSpec
- test suites
- descriptions
- tests
- examples
- assertions
- expectations
Test::Unit vs. RSpec
class FooTest
describe Foo
Test::Unit vs. RSpec
class FooTest
describe Foo
def test_bar
Test::Unit vs. RSpec
class FooTest
describe Foo
def test_bar
it "should do something"
Test::Unit vs. RSpec
class FooTest
describe Foo
def test_bar
it "should do something"
assert_xxx
Test::Unit vs. RSpec
class FooTest
describe Foo
def test_bar
it "should do something"
assert_xxx
object.should be_xxx
Test::Unit vs. RSpec
class FooTest
describe Foo
def test_bar
it "should do something"
assert_xxx
object.should be_xxx
def setup
Test::Unit vs. RSpec
class FooTest
describe Foo
def test_bar
it "should do something"
assert_xxx
object.should be_xxx
def setup
before :each do; end
Test::Unit vs. RSpec
class FooTest
describe Foo
def test_bar
it "should do something"
assert_xxx
object.should be_xxx
def setup
before :each do; end
def teardown
Test::Unit vs. RSpec
class FooTest
describe Foo
def test_bar
it "should do something"
assert_xxx
object.should be_xxx
def setup
before :each do; end
def teardown
after :each do; end
Live Coding Session
Filling out your RSpec toolbox
Scenarios
Scenarios
- Build records from scratch
Scenarios
- Build records from scratch
- create_model (returns the object)
Scenarios
- Build records from scratch
- create_model (returns the object)
- create_record (just creates)
Scenarios
- Build records from scratch
- create_model (returns the object)
- create_record (just creates)
- Add helpful methods to your specs/tests
Scenarios
- Build records from scratch
- create_model (returns the object)
- create_record (just creates)
- Add helpful methods to your specs/tests
- Create models with defaults -
create_user :name => "Sean"
Scenarios
- Build records from scratch
- create_model (returns the object)
- create_record (just creates)
- Add helpful methods to your specs/tests
- Create models with defaults -
create_user :name => "Sean"
- Do complicated setup -
login_as :admin
Scenarios
- Build records from scratch
- create_model (returns the object)
- create_record (just creates)
- Add helpful methods to your specs/tests
- Create models with defaults -
create_user :name => "Sean"
- Do complicated setup -
login_as :admin
- Specify scenario dependencies -
uses :posts
rcov
Did I test that or not?
heckle
Hurt your code for fun and profit
Fin