New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to simply stub objects #39
Comments
Yes, #stub! will do what you want. So you can say
Note that you are not able to access the stubbed object in this case, but
or if you expect to only ever have one method you are stubbing then you can
HTH. |
Thank you very much! And I suggest that this usage should be put into the README. |
Yeah it's actually in the API overview: https://github.com/rr/rr/blob/master/doc/03_api_overview.md#double-graphs. I need to clean this section up, though. |
@mcmire I still have problems here. |
Ah. Actually that makes sense now. You're right, #stub! does return a DoubleDefinition regardless of whether or not you give it a block. So, you want this instead:
...Only, this doesn't actually work, because I think there's a bug in RR that double-wraps a DoubleDefinition when using the block form. You have to say this instead:
For this reason I would recommend sticking with the inline form:
If you need to add more methods you can do that easily:
|
It's not perfect enough, but it works. Thanks for the help! |
Cool glad I could help. I might consider another way to make this more elegant in the future, Mocha and rspec-mocks definitely have RR beat here. |
@mcmire Yep, they do. But unfortunately, the inline form you recommended not work again now. I got these: 1) OrderCouponValidator#validate with order that has a coupon_code and the coupon code exists add errors to order if the coupon code
is not usable
Failure/Error: changes { {} }.
NoMethodError:
undefined method `changes' for #<RR::DoubleDefinitions::DoubleDefinition:0x007f9b0f28bb38>
# ./spec/models/order_coupon_validator_spec.rb:8:in `block (3 levels) in <top (required)>'
# ./spec/models/order_coupon_validator_spec.rb:20:in `block (4 levels) in <top (required)>' The mocking code is: let(:order) do
stub!.
errors { ActiveModel::Errors.new(Object.new) }.
changes { {} }.
subject
end I'm using Rails 3.2.13 btw. Just point out that problem. |
OMG I am so dumb. Of course that doesn't work. Okay let's try this again:
It isn't the most elegant thing in the world but I think that should actually work. |
Commenting on an old, old closed issue, but… So is the documentation wrong? When I try to use RR as suggested in the docs, eg. |
First, can you open a new issue with a script that shows the problem and is runnable? |
Every time i want to stub an object. I have to do like this:
Is there any simple way stub a object like what rspec mock does:
The text was updated successfully, but these errors were encountered: