Using Parts of RSpec With Other Test Frameworks

Sometimes, you’ll want to use the powerful test doubles available in rspec-mocks, or the composable matchers provided by rspec-expectations, in a project where the rest of RSpec isn’t a good fit. For instance, you may already have an extensive test suite written in Minitest, or you may be writing acceptance tests in Cucumber.[124][125]

Both of these parts of RSpec are easy to use with other test frameworks, or even without a test framework at all. In fact, you’ve already done so in Parts of an Expectation and throughout Chapter 13, Understanding Test Doubles.

If you’re using Minitest, RSpec provides a couple of conveniences for you, including the following:

  • Reporting unmet expectations correctly as Minitest assertion failures

  • Making sure RSpec’s expect method doesn’t clash with Minitest’s method of the same name

  • Verifying message expectations you’ve set on mock objects

To take advantage of this integration, require either or both of the following files in your tests:

 require ​'rspec/mocks/minitest_integration'
 require ​'rspec/expectations/minitest_integration'

Then, you can use RSpec’s test doubles and expectations freely in your Minitest suite:

 class​ DinosaurTest < Minitest::Test
 def​ test_dinosaurs_fly_rockets
  dinosaur = Dinosaur.new
  rocket = instance_double(Rocket)
 expect​(rocket).to receive(​:launch!​)
  dinosaur.fly(rocket)
 expect​(dinosaur).to be_excited
 end
 end

If you’re writing acceptance tests using Cucumber, you don’t need to do anything special to use RSpec-style expectations. Cucumber will detect when you’ve installed the rspec-expectations gem, and will automatically enable it.

We do not usually recommend using test doubles with the kinds of acceptance tests Cucumber is designed for, but Cucumber does offer rspec-mocks integration for those rare cases when you need it. To enable it, require ’cucumber/rspec/doubles’ in your environment setup.

To use parts of RSpec with another test framework, take a peek inside the two minitest_integration files from this example. They’ll give you a good starting point for integrating with your test framework.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset