-
-
Notifications
You must be signed in to change notification settings - Fork 399
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
Testing code with at_exit
conflicts with raise_error
#1117
Comments
For a more concrete example, I just published the gem in question, so you can dig into the source code too. |
👋 This is a tough one, your test is working correctly, because As far as I know there is no way to intercept the blocks and check them, and in any case would cause RSpec to exit uncleanly. |
A reply is given for the [RSpec issue](rspec/rspec-expectations#1117), I've reviewed the proposed solution and found that I'd take a different approach instead in order to fix the `at_exit` spec. First off, this commit extracts out the core logic for `behaves_like` into a `private` method. This is important for a couple of reason. By extracting it out, `behaves_like` now only consists of the `at_exit` logic, and because I can't test `at_exit` due to the nature of it, I've decided to test directly on the newly extracted `private` method. While it is generally frowned upon to test private methods (you should be testing the public interfaces only!!), and I do agree on that on a general case, I think that this is an exceptional situation that calls for a break of rules. Guidelines should be all that is, guides, it should not be rules that prevent innovation. Thus I've decided to break the rule this time. Fixes #2
Cool, thanks for confirming what I suspected! With that, I think I'm going to break the rules of My fix is over at edisonywh/behaves#16 along with my rationale, if someone is interested. Thanks for taking the time to answer my question, Jon! |
You could also test it by shelling out and running your code that way, which is how we test some parts of RSpec. |
I don't understand what you mean by |
Running your code as a seperate process on the command line and testing the resulting output. |
You could also experiment with |
A reply is given for the [RSpec issue](rspec/rspec-expectations#1117), I've reviewed the proposed solution and found that I'd take a different approach instead in order to fix the `at_exit` spec. First off, this commit extracts out the core logic for `behaves_like` into a `private` method. This is important for a couple of reason. By extracting it out, `behaves_like` now only consists of the `at_exit` logic, and because I can't test `at_exit` due to the nature of it, I've decided to test directly on the newly extracted `private` method. While it is generally frowned upon to test private methods (you should be testing the public interfaces only!!), and I do agree on that on a general case, I think that this is an exceptional situation that calls for a break of rules. Guidelines should be all that is, guides, it should not be rules that prevent innovation. Thus I've decided to break the rule this time. Fixes #2
Hey guys! I currently have a class like this:
The reason I am wrapping my code inside
at_exit
is because I want to run the execution of the code AFTER mybase
class has finished defining the methods. For example:I want to access
method_one
from inside my gem, I have two ways to do it:custom_method
all the way at the bottom of the filecustom_method
around anEND
block or anat_exit
block, both of which will ensure that mycustom_method
runs after the file thatextended
the file.I've decided to go with the second option because I do not want to put the method call at the end of the file.
It all works fine when I test it manually, however, the issue is as follow when I try to test it with rspec.
I want to test my method like so:
But this won't work, rspec will think that there's no error, I presume this is because my
at_exit
runs after the rspecraise_error
, which means the error is raised after the expectation.How can I make my test green? Appreciate any pointers!
The text was updated successfully, but these errors were encountered: