Comments (4)
Hello! I hope you're doing well. Thank you for raising this issue. After quickly reviewing the source code, I believe you can achieve the desired outcome by combining both where clauses within the conditions method, as shown in the example below.
it { should have_many(:active_website_resources)
.conditions(resource: { status: :active }, is_active: true)
.class_name('WebsiteResource') }
Please try using this code and see if it works as expected. Let me know if you need any further assistance!
from shoulda-matchers.
Hi!
I tried many combinations, inline and chained but the result is the same. The culprit is nested condition. Order of conditions matters but attribute names and values of nested condition do not.
Only is_active
is tested, nested attribute is ignored. Notice attribute changes and order:
# passes
it { should have_many(:active_website_resources)
.conditions(whatever: { whatever: :whatever }) # <==
.conditions(is_active: true)
.class_name('WebsiteResource') }
# fails
it { should have_many(:active_website_resources)
.conditions(is_active: true)
.conditions(whatever: { whatever: :whatever })
.class_name('WebsiteResource') }
Inline conditions:
# passes
it { should have_many(:active_website_resources)
.conditions(is_active: true, whatever: { whatever: :whatever })
.class_name('WebsiteResource') }
# fails
it { should have_many(:active_website_resources)
.conditions(is_active: false, whatever: { whatever: :whatever })
.class_name('WebsiteResource') }
# passes
it { should have_many(:active_website_resources)
.conditions(whatever: { whatever: :whatever }, is_active: true)
.class_name('WebsiteResource') }
# fails
it { should have_many(:active_website_resources)
.conditions(whatever: { whatever: :whatever }, is_active: false)
.class_name('WebsiteResource') }
from shoulda-matchers.
Yes, I dived into the code now, and we are using the where_values_hash
to build a hash of conditions and compare it with the given conditions for the matcher modifier.
Unfortunately, this method only returns conditions for the queried primary table, in this case, "website_resources", so any nested condition will be discarded. So, making this spec using shoulda is impossible for now 😭 .
We could implement a new solution to build the hash using some of the methods Rails uses on the where_values_hash
, but I'm not sure if that's a nice approach because they're internal and not even documented methods.
@mcmire I'd love to hear your thoughts on this.
from shoulda-matchers.
Thank you, @matsales28. I appreciate the detailed explanation.
from shoulda-matchers.
Related Issues (20)
- The library doesn't work with frozen-string-literals enabled globally. HOT 1
- Shoulda Matchers ActiveRecord Validate Uniqueness HOT 1
- validation_inclusion_of is returning ArgumentError HOT 1
- Minitest: is there a recommended way to use `shoulda-matchers` without using `shoulda-context` ? HOT 2
- Uniqueness matcher does not account for custom error messages HOT 2
- wrong constant name dummy value on rails 7 HOT 5
- rspec spec/unit fails with ruby 3.3.0dev
- Wrong error with matcher HOT 16
- Cannot validate numericality with dynamic values HOT 2
- Ruby-tests-explorer and shoulda integration HOT 1
- Add `query_constraints` qualifier to the association matcher.
- Numericality matcher not working as expected on 6.0.0 HOT 3
- Should implicit default values have valid expectations? HOT 4
- RangeError: cannot get the first element of beginless range
- polymorph uniq check fails with 6.2 HOT 7
- Sin
- Sinmime
- What Pieces of ActiveModel/ActiveRecord need to be present for shoulda to appear? HOT 2
- validate_presence_of tries to set the value for a read_only attribute 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 shoulda-matchers.