Comments (9)
Makes me wonder, what is the difference that makes RSpec load these files specifically from require statements?
RSpec adds ./lib
and (a configurable) spec path to the load path, this allows people working on normal ruby project to do require 'name'
in specs, and similarly allows require 'spec_helper'
to work.
This convention is sometimes also used for monkey patching as you say like lib/<gem>/<ext-file-with-same-name-as-original>.rb
to override or backport a fix, which typically isn't an issue either, as these files usually have the same contents as the original.
If it works from Rails, I take it you were requiring relatively or with full path names? As e.g. require 'date'
will only work once. Personally I would avoid shaddowing file names unless you are doing a full monkey patch with original contents, to ensure it will work reliably across tools.
from rspec-rails.
irb -f
Yep, works fine in irb:
➜ irb -f
irb(main):001:0> require 'date'
=> true
irb(main):002:0> Date.today + 1
=> #<Date: 2024-07-02 ((2460494j,0s,0n),+0s,2299161j)>
The error happens at:
alias_method :plus_without_duration, :+ # <-- here
alias_method :+, :plus_with_duration
Which is strange.
from rspec-rails.
Got it. Happy to have helped.
Is there anything actionable left?
from rspec-rails.
That’s weird. On 2.7.8 in irb -f
after require 'date'
I can Date.today + 1
. Can you?
Wondering what you’ll be able to find.
from rspec-rails.
Just for the reference https://github.com/rails/rails/blob/53410537594be0c0a528cff53dce433dd386cb6a/activesupport/lib/active_support/core_ext/date/calculations.rb#L97
from rspec-rails.
Grepped through our gems, disabled all the gems that extend the Date class, but didn't help.
Checked if the file where it fails is being loaded twice - and it isn't 🙃
from rspec-rails.
Can you set a binding.irb breakpoint just before that alias_method to see if new + 1
works?
from rspec-rails.
Can you set a binding.irb breakpoint just before that alias_method to see if
new + 1
works?
Yes, it fails on the missing plus.
I grepped the whole codebase and gems for the alias_method
with a :+
and the only thing I am getting is:
grep -r "alias_method :+" . -n
./.bundle/ruby/2.7.0/gems/nokogiri-1.15.5-x86_64-linux/lib/nokogiri/xml/node_set.rb:431: alias_method :+, :|
./.bundle/ruby/2.7.0/gems/activesupport-6.1.5/lib/active_support/core_ext/time/calculations.rb:289: alias_method :+, :plus_with_duration
./.bundle/ruby/2.7.0/gems/activesupport-6.1.5/lib/active_support/core_ext/date/calculations.rb:101: alias_method :+, :plus_with_duration
./.bundle/ruby/2.7.0/gems/xpath-3.2.0/lib/xpath/dsl.rb:65: alias_method :+, :union
./.bundle/ruby/2.7.0/gems/addressable-2.8.5/lib/addressable/uri.rb:1971: alias_method :+, :join
So what's happening doesn't make a lot of sense.
I checked RUBY_VERSION => "2.7.6"
Solution
Finally, I found out that we had a lib/date.rb
file, which somehow loaded the first time require 'date'
was run. (even before railties?)
But this only happens when running rspec, not when the rails app is run. Thank you so much for helping me figure this out!
from rspec-rails.
Turns out the project I work on has lib/<gem>/<ext-file-with-same-name-as-original>.rb
in a few places even.
And Rails has no problem with it, because it's only loaded explicitly from initializers.
Makes me wonder, what is the difference that makes RSpec load these files specifically from require statements?
from rspec-rails.
Related Issues (20)
- "expect to receive with" throws error when switching to Rails 7.1 HOT 17
- Rails/RedundantActiveRecordAllMethod false positive HOT 1
- Rails 7.1 upgrade: using deprecated preview_path= HOT 3
- Rails 7.1 raise WrongScopeError on take_failed_screenshot HOT 3
- be_routable not working in controller spec with Rails 7.1 HOT 2
- Wrap examples with Rails executor HOT 2
- Fixtures for namespaced models don't seem to work in rspec-rails 6.1.0 HOT 6
- Even if specify the use of TestUnit, it will be overwritten by Minitest and cannot be used. HOT 6
- stub_template isn't working as expected on latest version HOT 1
- render_template is not working HOT 12
- View example group inserts controller path into lookup_context in the wrong order HOT 2
- :git => "git://github.com/rspec/rspec.git" Not working anymore HOT 3
- rspec describe/context label within loop not woking HOT 5
- Controllers specs don't currently work on the Rails main branch (8.0.0.alpha) HOT 5
- Rack status code name changes breaks HttpStatusMatcher HOT 6
- Resetting `ActiveSupport::CurrentAttributes` can be inconsistent due to hook ordering HOT 14
- Private Method Conflict Between Helpers Causes Incorrect Method Call HOT 6
- No route matches when I run controller rspec ? HOT 1
- Support for encrypted attributes in fixtures cannot be enabled when using Rails 7.2.0 HOT 5
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 rspec-rails.