Testing with RSpec

RSpec is an alternative to MiniTest, which Rails uses. It’s different in almost every way, and many developers prefer it. Here’s what one of our existing tests might look like, written in RSpec:

 RSpec.​describe​ Cart ​do
 
  let(​:cart​) { Cart.​create​ }
  let(​:book_one​) { products(​:ruby​) }
  let(​:book_two​) { products(​:two​) }
 
  before ​do
  cart.​add_product​(book_one).​save!
  cart.​add_product​(book_two).​save!
 end
 
  it ​"can have multiple products added"​ ​do
  expect(cart.​line_items​.​size​).​to​ eq(2)
 end
 
  it ​"calculates the total price of all products"​ ​do
  expect(cart.​total_price​).​to​ eq(book_one.​price​ + book_two.​price​)
 end
 end

It almost looks like a different programming ...

Get Agile Web Development with Rails 6 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.