Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec 3 undefined method `allow' for #<RSpec::Core::ExampleGroup...>

describe '#messages' do
  subject do
    FactoryGirl.create :foo,
      :type => 'test',
      :country => 'US'
  end

  context 'when is not U.S.' do
    before{ allow(subject).to receive(:country).and_return('MX') }

    describe '#messages' do
      subject { super().messages }
      it { is_expected.to include 'This foo was not issued in the United States of America.' }
    end
  end
end

I'm trying to assign an attribute on the subject... I can't seem to get the incantation correct. Do I need a Double here? I'm not sure how that even works, and I apparently can't decipher the docs. Any help is appreciated.

like image 842
Dudo Avatar asked Nov 09 '22 23:11

Dudo


1 Answers

I think you should define the subject variable as a helper method using let. With this, you are defining a helper method that you can use everywhere in the file.

So, I'll modify your code as follows:

describe '#messages' do
  let(:subject) do
    FactoryGirl.create :foo,
      :type => 'test',
      :country => 'US'
  end

  context 'when is not U.S.' do
    before{ allow(subject).to receive(:country).and_return('MX') }

    describe '#messages' do
      subject { super().messages }
      it { is_expected.to include 'This foo was not issued in the United States of America.' }
    end
  end
end 
like image 152
Daniel Ruiz Avatar answered Nov 15 '22 04:11

Daniel Ruiz