vendredi 15 avril 2016

rspec model spec; testing if local var is assigned

I'd like to test in my model spec if the local var is set in the method. I tried to use assigns, but it doesn't work in model specs. What is the rails way to do this properly?

user.rb

def decreased_chat_number_pusher
  number = self.new_chat_notification #I wanna test if this is set
  Pusher.trigger_async('private-'+ self.id.to_s, 'new_chat_notification', { number: number })
end

user_spec.rb

  let(:user) { create(:user) }

  it "decreased_chat_number_pusher" do
    user.new_chat_notification = 3
    number = user.new_chat_notification
    #FOLLOWING LINE THROWS UNDEFINED METHOD: ASSIGNS
    expect(assigns(number)).to match(user.new_chat_notification)
    allow(Pusher).to receive(:trigger_async).with(('private-' + user.id.to_s), 'new_chat_notification', {number: number} )
    expect(Pusher).to receive(:trigger_async).with(('private-' + user.id.to_s), 'new_chat_notification', {number: number} )
    user.decreased_chat_number_pusher
  end

Aucun commentaire:

Enregistrer un commentaire