2016-04-14 4 views
0

Как проверить эти методы экземпляра с помощью rspec и factory?Методы проверки экземпляров rspec

фабрики

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 (здесь метод, который называется)

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 

сообщение об ошибке:

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

позвонить и проверить, что соответствующие изменения были сделаны? Не уверен, в каком аспекте вам нужна помощь с –

+0

. Впервые я делаю тесты, поэтому в основном весь подход для подобных методов экземпляра, где вы вызываете новые методы из метода экземпляра. Какие данные следует использовать? С чем я должен бороться? –

ответ

1

(Sidenote) Вам не нужно self в своих методах.

Посмотрите:

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 
+0

Я знаю Андрея, просто выглядит лучше: :). Хотя вы правы, я должен использовать его. –

+0

Андрей, по какой-то причине он не работает. Не могли бы вы угадать, основываясь на обновленном вопросе, где это происходит неправильно? –

+0

@SzilardMagyar Я показал вам схематически, как вы ожидаете, что вызов метода приведет к вызову другого метода. Вы должны настроить тестовых пользователей так, чтобы называть 'reduce_chat_notification_number' фактически было вызвано' check_and_decreasing_notification (уведомление) ', которое должно быть вызвано (что означает, что' notification.present? 'Должен возвращать' true'). Как это сделать, только вы знаете, потому что я не знаю логин за «notifications.between_chat_recipient (пользователь) .unchecked.first' и т. Д. –

Смежные вопросы