-
-
Notifications
You must be signed in to change notification settings - Fork 357
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
stub_const overwrites Class definition #1079
Comments
This is working by design: since I agree it's not ideal, but when RSpec is working with limited information (as it is in this case) it's not clear what the "right" thing is.
RSpec does not do any autoloading. My rule of thumb is: if I want something loaded, I load it by requiring it. Just add a There was talk at some point about potentially adding some autoloading for this kind of situation in rspec-rails since that's a rails context where autoloading is common, but I don't remember what conclusion we arrived at and I can't find the issue. @JonRowe do you remember? |
We talked about it but never did anything about it, my suggested work around at the time was to do |
That's exactly the workaround I used as well |
* I was observing this problem: rspec/rspec-mocks#1079 when running a feature spec in isolation. ``` 1) School viewing public listings when signed in with DfE Sign In A signed in school should see a link back to their own dashboard when viewing public listings Failure/Error: validates :minimum_salary, salary: { presence: true, minimum_value: true } NoMethodError: undefined method `new' for SalaryValidator:Module ```
* I was observing this problem: rspec/rspec-mocks#1079 when running a feature spec in isolation. ``` 1) School viewing public listings when signed in with DfE Sign In A signed in school should see a link back to their own dashboard when viewing public listings Failure/Error: validates :minimum_salary, salary: { presence: true, minimum_value: true } NoMethodError: undefined method `new' for SalaryValidator:Module ```
* I was observing this problem: rspec/rspec-mocks#1079 when running a feature spec in isolation. ``` 1) School viewing public listings when signed in with DfE Sign In A signed in school should see a link back to their own dashboard when viewing public listings Failure/Error: validates :minimum_salary, salary: { presence: true, minimum_value: true } NoMethodError: undefined method `new' for SalaryValidator:Module ```
thanks for the workaround, more than 3 years after @JonRowe post, it still works! 🤔 |
…the const is stubbed otherwise the controller will be broken See details here: rspec/rspec-mocks#1079 (comment)
When using stub_const before the Class containing the constant has been loaded, a module is automatically created with the name.
Here is a minimal working example (on Rails file structure):
app/models/model.rb
:spec/models/model_spec.rb
:And that test fails with:
I think in that case the Module or Class should be loaded and an exception raised in case it doesn't exists or the original constant doesn't (The same way using
expect(Class).to receive(:method)
fails if method doesn't exist).I'm using rspec-mocks 3.3.2 with rails 4.2.3 and haven't tried it with the master branch, but nothing appears in the changelog.
The text was updated successfully, but these errors were encountered: