0

How can I test these instance methods with rspec and factory?

factories

FactoryGirl.define do
  factory :user do
    sequence(:email) { |n| "example#{n}@gmail.com"
    password 'example0000'
    password_confirmation 'example0000'
    new_chat_notification { Faker::Number.between(0, 10) }
    new_other_notification { Faker::Number.between(0, 10) } 
  end

  factory :notification do
    notifiable_id { Faker::Number.between(1, 10) }
    notifiable_type { Faker::Lorem.word }
    action { Faker::Hipster.word }
    checked_at { Faker::Time.between(DateTime.now - 2, DateTime.now - 3)  }
    association :sender, factory: :user
    association :recipient, factory: :user
  end
end

user.rb

#check and decrease chat notification that happens between 2 given users (max 1)
def decreasing_chat_notification_number(user)
  notification = self.notifications.between_chat_recipient(user).unchecked.first
  self.checking_and_decreasing_notification(notification) if notification.present?
end

#check and decrease task notifications that happens between 2 given users
def decreasing_task_notification_number(user)
  self.notifications.task.between_other_recipient(user).unchecked.each do |notification|
    self.checking_and_decreasing_notification(notification)
  end
end

UPDATE

user.rb (here is the method that is called)

def checking_and_decreasing_notification(notification)
  notification.check_notification
  if notification.notifiable_type == "Message"
    decrease_new_chat_notifications
    decreased_chat_number_pusher
  else
    decrease_new_other_notifications
    decreased_other_number_pusher
  end
end

user_spec.rb

let(:sender) { create(:user) }
let(:user) { create(:user) }
let(:notification) { create(:notification, notifiable_type: "Message", recipient: user, sender: sender) }

it "decreasing_chat_notification_number" do
  allow(user).to receive(:checking_and_decreasing_notification).with(notification)
  user.decreasing_chat_notification_number(sender)
  expect(user).to receive(:checking_and_decreasing_notification).with(notification)
end

error message:

1) User instance methods decreasing_chat_notification_number
 Failure/Error: expect(user).to receive(:checking_and_decreasing_notification).with(notification)

   (#<User:0x007fefcfd6ce20>).checking_and_decreasing_notification(#<Notification id: 1, 
     recipient_id: 1, created_at: "2016-04-14 19:47:36", updated_at: "2016-04-14 19:47:36", 
     sender_id: 2, checked_at: "2016-04-12 02:32:50", notifiable_id: 4, notifiable_type: "Message", action: "tilde">)
   expected: 1 time with arguments: (#<Notification id: 1, 
     recipient_id: 1, created_at: "2016-04-14 19:47:36", updated_at: "2016-04-14 19:47:36", 
     sender_id: 2, checked_at: "2016-04-12 02:32:50", notifiable_id: 4, notifiable_type: "Message", action: "tilde">)
   received: 0 times
2
  • 2
    Call them and check that the appropriate changes were made? Not sure what aspect you need help with Commented Apr 14, 2016 at 17:16
  • My first time doing tests, so basically the whole approach for this kinda instance methods where you invoke new methods from the instance method. What data should I use? What should I check against ? Commented Apr 14, 2016 at 17:37

1 Answer 1

1

(sidenote) You do not need self in your methods.

Take a look:

def decreasing_chat_notification_number(user)
  notification = notifications.between_chat_recipient(user).unchecked.first
  checking_and_decreasing_notification(notification) if notification.present?
end


describe '#decreasing_chat_notification_number' do
  let(:notification) { create(:notification) }
  let(:user)         { create(:user) }
  subject            { create(:user) }

  it 'your description' do
    expect(subject).to receive(:checking_and_decreasing_notification).with(notification)
    subject.decreasing_chat_notification_number(user)
  end
end
Sign up to request clarification or add additional context in comments.

11 Comments

I know Andrey, just looks better this way :). Although you are right I should get used it.
Andrey, for some reason it's not working. Could you guess based on the updated question where it goes wrong?
@SzilardMagyar I showed you schematically how you'd expect the method call to result in calling another method. You have to set up the test users so that calling decreasing_chat_notification_number in fact resulted in checking_and_decreasing_notification(notification) to be called (meaning notification.present? should return true). How to do that only you know, because I do not know the login behind the notifications.between_chat_recipient(user).unchecked.first etc..
Thanks Andrey! I thought the error may happened cuz I missed something not because of my data setup.
@SzilardMagyar no probs, hope you'll succeed in setting up the test data. You might also want to take a look at rspec docs
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.