Shoulda-matchers: NoMethodError: undefined method `optional'

Created on 25 Dec 2017  路  15Comments  路  Source: thoughtbot/shoulda-matchers

minitest (5.10.3, 5.10.1)
shoulda-matchers (3.1.2)
require "test_helper"

describe Event do
  subject { Event.new }
  it { must belong_to(:visit).optional }
end

=>

NoMethodError: undefined method `optional' for #<Shoulda::Matchers::ActiveRecord::AssociationMatcher:0x00007f96daad39f0>
Did you mean?  options

How to use optional matcher in my case?

Most helpful comment

@dgorodnichy @Maychell Oh, derp -- this is only available on the master branch, this isn't in a release just yet.

All 15 comments

@dgorodnichy Hmm, that's definitely very strange. Are you sure you're using 3.1.2? Where is describe coming from if you're using Minitest -- are you using Minitest::Spec?

@mcmire, Yes, I use Minitest::Spec. I'm sure that shoulda-matchers version is 3.1.2

Same thing here.
My class:

class User
  belongs_to :address, optional: true
end

Test:

RSpec.describe User, type: :model do
  context 'associations' do
    it { is_expected.to belong_to(:address).optional }
  end
end

Result:

1) User associations
     Failure/Error: it { is_expected.to belong_to(:address).optional }

     NoMethodError:
       undefined method `optional' for #<Shoulda::Matchers::ActiveRecord::AssociationMatcher:0x007fa266945fd8>
       Did you mean?  options

@dgorodnichy @Maychell Oh, derp -- this is only available on the master branch, this isn't in a release just yet.

@mcmire so do you suggest to use master branch instead of stable 3.1.2 release? I also experience this issue.

Upvote this too.

@mkalygin @jasonperrone Yes, I'd advise using the master branch if you really need this.

Upvote!

Upvote too :3 using master atm.

Upvote, waiting for the stable release :) :+1:

When do you expect to release a stable version with the optional method? 馃檹

also waiting for this

Hi!
The master branch is pretty stable. Just point the Gemfile to it 馃檪
Cheers!

is there an estimated release date for 4.0.0?

Hi @kleinjm

We still have some open issues for 4.0.0.

You can follow the progress and find issues to contribute for the next verion at:
https://github.com/thoughtbot/shoulda-matchers/milestone/13

Was this page helpful?
0 / 5 - 0 ratings