Comments (9)
I didn't know this, but that explains it.
I'm trying to add tests for how many emails are sent and who they are sent to, so I was using UserMailer.expects(:comment_notification).with(...).once
The mailer stuff calls UserMailer.comment_notification.deliver, which of course throws a null pointer in this case. I guess it doesn't make sense to me that .expects also stubs the method, because that could throw wrenches up an application in any number of ways, and prevents you from doing a lot of tests.
Can I have it just call through to the original method? This would be a lot less intrusive.
I ended up defining helper methods and tracking through those.
from mocha.
This is the intended behaviour of Mocha. When you call UserMailer.expects(:comment_notification)
, the UserMailer.comment_notification
method is replaced by a stub method for the rest of the test. The default is for this stub to return nil
, unless you use Mocha::Expectation#returns
, or Mocha::Expectation#raises
.
It would be useful to understand what were you expecting to happen so that I can try and improve the documentation if it is lacking. Were you expecting the method to still execute the original UserMailer.comment_notification
method and still return the same return value as if you had not called UserMailer.expects
?
Thanks, James.
from mocha.
Even that though is not ideal.
I want to BOTH count the method calls AND run the mailer code. Right now mail templates are not being tested in this case.
from mocha.
Thanks for clarifying what you were expecting to happen. I will try and improve the documentation for the expects
method to make it clearer what will happen.
In general the idea of Mock Objects is to isolate the object (or objects) under test from its (or their) collaborators. In almost all mocking frameworks you will find that setting an expectation also creates a stub implementation. The idea is that you have total control over this stub implementation and can provide canned return values specified in the test.
When you say "mail templates are not being tested", are you saying that you want them to be tested, but the fact that Mocha is intercepting the call to UserMailer.comment_notification
means that they are not being tested?
If so, I'd be inclined to split the test into two types of test - one which uses Mocha to set up expectations for the calls to UserMailer
and another which makes real calls to UserMailer
and checks the templates are rendered without an error.
If you are set on the idea of counting the number of calls, but still calling the real implementation, I believe Brian Takita's RR library will do what you want (see the section headed mock.proxy).
Alternatively, you could use ActionMailer
with its delivery_method
set to :test:
and inspect ActionMailer.deliveries
.
I hope that helps.
from mocha.
I've created this Lighthouse ticket to deal with improving the documentation. Please close this Issue if you are satisfied with the response.
Regards, James.
from mocha.
Thanks James. I can see how isolating would be good, however I generally prefer broader tests to catch any template errors.
from mocha.
Since that's a subjective issue of style, I think we're going to have to agree to differ. But I think the only change I'm going to make to Mocha is to improve the documentation as described above. So for the moment, I'm going to close this issue.
from mocha.
Sure, no problem. Seems like an expects that also calls through would be a nice new feature, particularly in a case like this, where the return value from the method you want to count is used by third party code.
from mocha.
Cool. There's already a Lighthouse ticket for the feature you describe in case you want to voice your support, further explain the motivation for the feature, etc.
from mocha.
Related Issues (20)
- Config option for blocking mocks/stubs on specific methods or classes HOT 1
- Mocha is incompatible with minitest v5.19 and later HOT 16
- Replace references to `MiniTest` with `Minitest` and generally tidy this stuff up HOT 1
- Consider adding an option to disable backtrack filtering
- Unstub in the `then` clause
- Stub by id
- Mocha::StateMachine states method stomps on states fixture HOT 8
- Stubs on instances of DelegateClass do not behave as expected HOT 9
- Add docs about issues & workarounds for using Mocha with BasicObject & the various Delegator classes
- YARD docs for Mock#method_missing is broken HOT 1
- Update Ruby Hash syntax in docs
- Drop support for Ruby v2.0
- Drop support for Ruby v2.1
- Document some of the history of Mocha
- Improve failure message
- sometimes method fails to be mocked HOT 8
- Sign releases
- Stubbed methods not cleaned up post spec completion HOT 1
- Consider using GitHub-flavoured markdown notes, warnings, etc in README
- Mocha and Rails 7.0+ and controller tests HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mocha.