File tree 2 files changed +33
-1
lines changed
2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
10
10
For a steady stream of TILs from a variety of rocketeers, checkout
11
11
[ til.hashrocket.com] ( https://til.hashrocket.com/ ) .
12
12
13
- _ 822 TILs and counting..._
13
+ _ 823 TILs and counting..._
14
14
15
15
---
16
16
@@ -648,6 +648,7 @@ _822 TILs and counting..._
648
648
- [ Last Raised Exception In The Call Stack] ( ruby/last-raised-exception-in-the-call-stack.md )
649
649
- [ Limit Split] ( ruby/limit-split.md )
650
650
- [ Listing Local Variables] ( ruby/listing-local-variables.md )
651
+ - [ Mock Method Chain Calls With RSpec] ( ruby/mock-method-chain-calls-with-rspec.md )
651
652
- [ Mocking Requests With Partial URIs Using Regex] ( ruby/mocking-requests-with-partial-uris-using-regex.md )
652
653
- [ Navigate Back In The Browser With Capybara] ( ruby/navigate-back-in-the-browser-with-capybara.md )
653
654
- [ Next And Previous Floats] ( ruby/next-and-previous-floats.md )
Original file line number Diff line number Diff line change
1
+ # Mock Method Chain Calls With RSpec
2
+
3
+ Generally with RSpec you mock one method call at a time:
4
+
5
+ ``` ruby
6
+ allow(User ).to receive(:new ).and_return(true )
7
+ ```
8
+
9
+ Sometimes you are dealing with code that involves a chain of method calls.
10
+
11
+ ``` ruby
12
+ User
13
+ .new
14
+ .approve
15
+ .send_welcome_email
16
+ ```
17
+
18
+ If it becomes unreasonable to mock out each individual method, you can instead
19
+ mock out the chain of calls.
20
+
21
+ ``` ruby
22
+ allow(User ).to receive_message_chain(' new.approve.send_welcome_email' )
23
+ ```
24
+
25
+ Alternatively, you can write this as:
26
+
27
+ ``` ruby
28
+ allow(User ).to receive_message_chain(:new , :approve , :send_welcome_email )
29
+ ```
30
+
31
+ [ source] ( https://relishapp.com/rspec/rspec-mocks/docs/working-with-legacy-code/message-chains )
You can’t perform that action at this time.
0 commit comments