thoughtbot / shoulda-matchers

Simple one-liner tests for common Rails functionality
https://matchers.shoulda.io
MIT License
3.53k stars 909 forks source link

Minitest: is there a recommended way to use `shoulda-matchers` without using `shoulda-context` ? #1571

Open lioneldebauge opened 1 year ago

lioneldebauge commented 1 year ago

Hello 👋

My team is looking into adding shoulda-matchers to our Minitest test suite. Is it possible to use shoulda-matchers without shoulda-context gem ? If so what is the recommended way ?

Documentation suggests it in "Getting Started" section below but all examples assume the usage of should method.

https://matchers.shoulda.io/docs/v5.3.0/#minitest

Thanks for any help that you would be willing to give us and for this very useful gem 🙏

mcmire commented 1 year ago

Hi @lioneldebauge, sorry for the delay. It is possible to use this without shoulda-context, yes. I haven't tried it, but others have found success using the minitest-matchers_vaccine gem. See: https://github.com/thoughtbot/shoulda-matchers/issues/825#issuecomment-276866714.

Hope that helps!

lioneldebauge commented 1 year ago

@mcmire you just made my day. Thanks a lot for your answer ! Should we modify the documentation to include a reference ? I can open a PR if needed.


For reference for others who might need it. I'm using Minitest::Spec::DSL

# in a helper file

def assert_must(matcher, subject, msg = nil)
  assert matcher.matches?(subject), msg
end

# in a model file

describe("validations")
  let(:user) { User.new }

  it { assert_must validates_presence_of :name, user }
end