RSpec: Expecting multiple calls of the same method
If the argument list is the same every time:
expect(object).to receive(:foo).with('argument').and_return('response 1', 'response 2')
If the argument list differs between invocations:
expect(object).to receive(:foo).with('argument 1').ordered.and_return('response 1')
expect(object).to receive(:foo).with('argument 2').ordered.and_return('response 2')
Related cards:
RSpec: Leverage the power of Capybara Finders and Matchers for view specs
View specs are a powerful tool to test several rendering paths by their cases instead of using a more costing feature spec. This is especially useful because they become quite co...
The Ruby Object Model
In Ruby (almost) everything is an Object
. While this enables a lot of powerful features, this concept might be confusing for developers who have been programming in more static languages, such as Java or C#. This card should help understanding t...
Heads up: expect(object).to receive(:method_name) does not execute the original implementation of the method
Let's assume that we have a model Movie
that registers a callback function when a new instance of Movie
is created (Note: For the purpose of this card it is not important what that callback does or which type of callback it is).
This is how...
RSpec: Expect one of multiple matchers to match
RSpec let's you chain a matcher with .or
. The expectation will then pass if at least one matcher matches:
expect(color).to eq("red").or eq("green")
Real-world example
A real-world use case would be to test if th...
CSS: Matching against attributes and their values (or parts of them)
You probably know that you can use CSS selectors to match against elements and their attributes, such as:
a[title] { /* any <a> that has a "title" */ }
a[data-fancy="true"] { /* any <a> that has their "data-fancy" attribute set to "true" *...
The JavaScript Object Model: A deep dive into prototypes and properties
Speaker today is Henning Koch, Head of Development at makandra.
This talk will be in German with English slides.
Introduction
As web developers we work with JavaScript every day, even when our backend code uses anothe...
You can use any RSpec matcher to match arguments of method calls
RSpec lets you define the arguments with which you expect a method to be invoked:
subject.should_receive(:say).with('hello')
Sometimes you don't care about the exact arguments. For such cases RSpec comes with [argument constraints](https...
RSpec: Ensuring a method is called on an object that will be created in the future
rspec >= 3.1 brings a method and_wrap_original
. It seems a bit complicated at first, but there are use cases where it helps to write precise tests. For example it allows to add expectations on objects that will only be created when your code is ...
RSpec and Cucumber: Shorthand syntax to run multiple line numbers in the same file
This works in modern RSpecs (RSpec >= 2.x) and Cucumbers:
rspec spec/models/node_spec.rb:294:322
cucumber features/nodes.feature:543:563:579
Also your features should be shorter than that :)
Output the descriptions of RSpec examples while they are running
In order to
- track down warnings and to see failing specs immediately
- or to get an overview of the core functionalities,
you can use RSpec's "nested" format. It looks like this:
Tool
validations
should require model to be ...