У меня есть метод, который выглядит следующим образом:Rspec как проверить в нагнетающей методе
def self.average_top_level_comments_leaders
top_level_comment_count = CrucibleComment.group(:user_id).where(parent_comment_id: nil).order('count_all DESC').count
code_review_assigned_count = Reviewer.group(:user_id).order('count_all DESC').count
division_result = top_level_comment_count.inject({}) do |result, item|
id = item.first #id =12
count = item.last #value = 57
if (count && code_review_assigned_count[id])
result[id] = (count/ code_review_assigned_count[id]).round(2)
#result[12] = 57/12 = 3.3, => {12, 3.3}
end
result
end
end
Этого метод возвращает хэш с идентификаторами в качестве ключей и результатов деления, как значения.
Я успешно протестировал top_level_comment_count и code_review_assigned количество, но у меня возникают проблемы, выяснить, как я могу проверить 4 другие вещи, которые находятся в блоке сделать:
.first, .last, .round(2), result
Я пытаюсь проверить .first и это то, что у меня есть до сих пор:
describe '#average_top_level_comments_leaders' do
subject { User.average_top_level_comments_leaders}
let(:avg_top_level_comments) { double }
let(:code_review_count) { double }
let(:item) { double({id: 12}) }
context 'when getting the comment count succeeds ' do
before do
allow(CrucibleComment).to receive(:group).with(:user_id).and_return(avg_top_level_comments)
allow(avg_top_level_comments).to receive(:where).with(parent_comment_id: nil).and_return(avg_top_level_comments)
allow(avg_top_level_comments).to receive(:order).with('count_all DESC').and_return(avg_top_level_comments)
allow(avg_top_level_comments).to receive(:count).and_return(avg_top_level_comments)
allow(avg_top_level_comments).to receive(:inject).and_return(avg_top_level_comments)
allow(item).to receive(:first).and_return(item)
allow(Reviewer).to receive(:group).with(:user_id).and_return(code_review_count)
allow(code_review_count).to receive(:order).with('count_all DESC').and_return(code_review_count)
allow(code_review_count).to receive(:count).and_return(code_review_count)
allow(code_review_count).to receive(:round).with(2).and_return(code_review_count)
end
it 'and the correct parameters are called' do
expect(CrucibleComment).to receive(:group).with(:user_id)
subject
end
it 'and comment count is calling descending correctly' do
expect(avg_top_level_comments).to receive(:order).with('count_all DESC')
subject
end
it 'item gets the first result' do
expect(item).to receive(:first)
subject
end
end
end
Я не могу получить последнее заявление. Я пытаюсь ожидать (item) .to получать (: first), но он говорит об этом в ошибке:
Ошибка/ошибка: ожидать (item) .to receive (: first) (Double) .first (* (любые аргументы)) Ожидаемое: 1 раз с любыми аргументами получено: 0 раз с любыми аргументами
Любая идея, почему это не проходит? Два других его проходит
ИМХО, вы тесты слишком плотно связаны. Вместо того, чтобы все обрезать, настройте данные так, чтобы вызов возвращал то, что вы знаете, и должен это делать. Вы не хотите, чтобы ваш тест терпел неудачу, потому что я изменил «round» на нечто эквивалентное (например, '(... + 0.5) .floor' или что-то еще). –
@PhilipHallstrom Теперь, когда я думаю об этом, я просто хочу проверить «результат». Как я могу это проверить? – Jay266