Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: can't define singleton (RSpec)

This might look like a newbie issue but I'm stumped. I'm trying to build a simple Ruby slack service and I'm putting together some unit tests using RSpec. I bumped into this strange issue and I can't see what's going on.

RSpec.describe SlackService do
  let(:token) { 'BOT-TOKEN' }
  subject do
    SlackService.new(token)
  end

  describe '#channel_list' do
    context 'get channels' do
      let(:client) { instance_double(Slack::Web::Client) }
      before(:each) do
        allow(:subject).to receive(:client) { client }
        @result = subject.channel_list
      end
      it { expect(@result).to eq [] }
    end
  end
end

The error I get when I run rspec is

Failures:

  1) SlackService#channel_list get channels
     Failure/Error: allow(:subject).to receive(:client) { instance_double(Slack::Web::Client) }

     TypeError:
       can't define singleton
     # ./spec/slack_service_spec.rb:12:in `block (4 levels) in <top (required)>'

Will edit if someone needs more information. Any idea what this error means? I can't seem to get rid of it no matter what.

like image 548
Jay Kannan Avatar asked Nov 02 '17 17:11

Jay Kannan


1 Answers

It seems to be a typo. Should be subject and not :subject

allow(subject).to receive(:client) { client }
like image 74
Norly Canarias Avatar answered Oct 20 '22 08:10

Norly Canarias