From the course: Ruby: Testing with RSpec

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Message count constraints

Message count constraints - Ruby Tutorial

From the course: Ruby: Testing with RSpec

Start my 1-month free trial

Message count constraints

- Message Count Constraints are another type of modifier to message expectations. They let you verify how many times a method gets called during the course of the example. So for example, let's say that I have a mock object for a BlogPost. I can then set an expectation that the post will receive the (:like) message. I can further modify that though and say, not only should it receive it, but it should receive it exactly three times. .exactly and then the number of times, followed by .times afterwards. So then in order to satisfy my example, I proceed to take that mock object and call (:like) on it three different times passing in a different user each time, as presumably liking this blog post. In addition to having exactly three times, we also have some convenience methods for once or twice. Those get used so often that we have shortcuts for those. Here's another example: Let's say that I have an actual cart object and it has some complexity built into it. I have the ability to add…

Contents