You can fix this by making the mock return a result.Įxpect(mock).to receive(:flip).with("ruby.jpg").and_return("ruby-flipped.jpg") ![]() If the code is expecting some value other than nil then this will result in an error. When your code calls flip, the mock will return nil. The ImageProcessor class does not implement the instance method: flipīut it will work correctly if the method exists. This will raise an error if the method doesn’t exist: One of the downsides of mocks & stubs is that you can end up using a method that doesn’t exist in your production code.īecause the method name has changed… or maybe you made a typo!Ī verified double can be used as either a stub ( allow) or a mock ( expect) & it will check that a method with this name exists. That’s the difference! How to Use Verified Doubles We are allowing our testing object double("json") to receive & respond to this method, but we aren’t checking if it’s being called. The allow method is what makes this a stub. A mock expects methods to be called, if they are not called the test will fail.A stub is only a method with a canned response, it doesn’t care about behavior.“Did we tell others the right thing to do, with the right information, and exactly the right amount of times that we need?”Īnother confusion point is about comparing mocks & stubs. When using a mock you are testing the behavior: “Did this method return a flipped image?” In a regular test you check the return value of a method: We use a mock to test if this method was called once, with an argument.īut you can imagine a complete implementation of ImageFlipper that checks if the file exists, if it’s a valid image, etc. This processor responds to the flip method. We have created a ImageFlipper class that takes an image_processor. You can make this test pass by giving it what it wants:ĭef there you go, we have a passing test:įinished in 0.00751 seconds (files took 0.11157 seconds to load) This is saying that the flip method was called 0 times, but it was expected to be called 1 time. rspec-mocks.rb:6:in `block (2 levels) in ' With this test we can write our code using TDD.ġ) ImageFlipper calls the flip method with the correct argumentsįailure/Error: expect(mock).to receive(:flip).with("ruby.jpg")Įxpected: 1 time with arguments: ("ruby.jpg") It "calls the flip method with the correct arguments" doĮxpect(mock).to receive(:flip).with("ruby.jpg") Time for some code examples! RSpec Mock Examples Mocks replace the original object, so the real methods won’t be called. This means that you won’t be flipping images (a slow operation) every time you run your tests. You want to test the interaction between your code & this external dependency… so you write a mock that expects the correct methods to be called on the ImageProcessor class. Instead of writing your own image-manipulation code you use a gem like mini_magick. Instead of testing the output value, like in a regular expectation. You use mocks to test the interaction between two objects. ![]() (Or a mock in general, because this isn’t a concept unique to RSpec.)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |