RSpec. Testing with RSpec Test::Unit “does the job”, but it would be nice if tests would be more...

10
RSpec

Transcript of RSpec. Testing with RSpec Test::Unit “does the job”, but it would be nice if tests would be more...

Page 1: RSpec. Testing with RSpec Test::Unit “does the job”, but it would be nice if tests would be more descriptive, more Enlgish-like RSpec uses some Ruby magic.

RSpec

Page 2: RSpec. Testing with RSpec Test::Unit “does the job”, but it would be nice if tests would be more descriptive, more Enlgish-like RSpec uses some Ruby magic.

2

Testing with RSpec

• Test::Unit “does the job”, but it would be nice if tests would be more descriptive, more Enlgish-like

• RSpec uses some Ruby magic to provide a simple DSL syntax for writing specs/examples (tests)

• The writing of the tests is more intuitive as well as the output from running the tests

Page 3: RSpec. Testing with RSpec Test::Unit “does the job”, but it would be nice if tests would be more descriptive, more Enlgish-like RSpec uses some Ruby magic.

3

Installing Rspec

• Create a spec directory and place your “specs” there

Page 4: RSpec. Testing with RSpec Test::Unit “does the job”, but it would be nice if tests would be more descriptive, more Enlgish-like RSpec uses some Ruby magic.

4

describe()

• Set of related tests (a.k.a. example group)• Takes either a String or Class as argument• Describe blocks can be nested• All specs must be inside a describe block• Automagically creates a class where all the

action happens (unlike Test::Unit which always subclasses TestCase class)

Page 5: RSpec. Testing with RSpec Test::Unit “does the job”, but it would be nice if tests would be more descriptive, more Enlgish-like RSpec uses some Ruby magic.

5

before() and after() methods

• before() and after() methods are similar to setup() and teardown() in Test::Unit

• Can pass in either :each or :all (infrequently used) to specify whether the block will run before/after each test or once before/after all tests

• before :all could be useful, if for example you only want to connect to DB once

Page 6: RSpec. Testing with RSpec Test::Unit “does the job”, but it would be nice if tests would be more descriptive, more Enlgish-like RSpec uses some Ruby magic.

6

it() method

• Used to define the actual RSpec specifications/examples

• Takes an optional string that describes the behavior being tested

• specify() method is an alias of it()• Usually, specify() is used for one-line tests

and it() is used otherwise; but it really depends on what reads better for a particular test and its output

Page 7: RSpec. Testing with RSpec Test::Unit “does the job”, but it would be nice if tests would be more descriptive, more Enlgish-like RSpec uses some Ruby magic.

7

Pending tests

• If you are not ready to implement certain behavior, but would like to put a test (not to forget to test it in the future) – you can put it in and just omit the actual test block

• Or you can call pending() method inside the block and RSpec will mark the method as pending

Page 8: RSpec. Testing with RSpec Test::Unit “does the job”, but it would be nice if tests would be more descriptive, more Enlgish-like RSpec uses some Ruby magic.

8

calculator.rb

Page 9: RSpec. Testing with RSpec Test::Unit “does the job”, but it would be nice if tests would be more descriptive, more Enlgish-like RSpec uses some Ruby magic.

9

calculator_spec.rb

Page 10: RSpec. Testing with RSpec Test::Unit “does the job”, but it would be nice if tests would be more descriptive, more Enlgish-like RSpec uses some Ruby magic.

10

Default and documentation formats