shoulda: undefined method `validate_presence_of' in shoulda 3.0.1, Rails 3.2 and RSpec-Rails 2.8.1
Seem to be unable to fix an error saying:
Failure/Error: it { should validate_presence_of(:username) }
NoMethodError:
undefined method `validate_presence_of' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007fee4f3aa9b8>
Also using ruby-1.9.2
About this issue
- Original URL
- State: closed
- Created 12 years ago
- Comments: 78 (1 by maintainers)
Commits related to this issue
- Bundle rspec explicitly to fix shoulda-matchers issues shoulda-matchers will not always register itself correctly when rspec is not declared explicitly as a test dependency, see: https://github.... — committed to scube-dev/scube-server by deleted user 9 years ago
- update depreciations, continue resolving tests; see #135 Capybara fix: https://github.com/jnicklas/capybara/issues/1592#issuecomment-152199620 Shoulda fix: https://github.com/thoughtbot/shoulda/issue... — committed to wearefine/fae by tshedor 8 years ago
- Update development and test gems * Ran into an issue with shoulda-matchers * Took a suggestion from https://github.com/thoughtbot/shoulda/issues/203 which seems like a fine workaround for now. Requ... — committed to sophomoric/secret by AdrianCann 8 years ago
- Update development and test gems (#52) * Ran into an issue with shoulda-matchers * Took a suggestion from https://github.com/thoughtbot/shoulda/issues/203 which seems like a fine workaround for ... — committed to sophomoric/secret by AdrianCann 8 years ago
It works for me:
Thanks @zekefast
rails 4.2.4 shoulda matchers 3.0.1 all fixes after adding to
spec\spec_helper.rb
It seems that reverting
shoulda-matcher
back to version 2.5.0 fix this.After everything, it still wouldn’t work in Rails 4.2+. I had to add
type: :model
.Same here:
2.2.3
,3.3.0
4.2.4
4.2.4
it works for
2.8.0
and does not work with3.0.0
.Configuration for
2.8.0
Configuration for
3.0.0
After some hours of research here’s a solution that worked for me.
should go after
in
rails_helper.rb
(not at the top ofspec_helper.rb
).I’m confused why this issue has been going for so long and through so many different versions of the gem and Rails. I see people doing some weird require order hacking in order to get it to work. I used @bsodmike’s trick (
RSpec.describe Foo, type: :model do
) but effectively in Rails 4.2.6 with the latest version (3.1.1), this gem is broken by default.@stefanhendriks - the shoulda-matchers method is
validate_presence_of
, notvalidates_presence_of
. And I’m pretty sure it needs to be within anit
block, i.e.:Setting for rails 4.2.5 and ruby 2.2.4
Gemfile
spec/shoulda_matchers_helper.rb
spec/rails_helper.rb
Using RoR 4.2.6 this works for me.
@wbigal your fix worked for me. Can we add that to the documentation?
Same problem here with Rails 4.2, shoulda-matchers 2.8.0, rspec 3.2.
None of the following solutions have worked for me:
Here’s my Gemfile:
and my spec_helper.rb:
Reverse shoulda-matcher to 2.5.0 fix the issue for me also, thanks @hieuk09 .
Check https://github.com/thoughtbot/shoulda-matchers for configuration block to place in rails_helper.rb
@karlingen, @wbigal The issue was fixed for me, by adding
type: :model
tag to tests. More details here.I was able to resolve this in another project by adding
gem 'shoulda-matchers', require: false
to theGemfile
, and then inside ofspec_helper
addingrequire 'shoulda/matchers'
.