我正在对用户的馈送进行分页,并想模拟我正在使用的API的响应。API可能会返回奇怪的结果,因此我希望确保如果API返回我已经看到的项,请停止分页。在第一次调用方法get_next_page时,我使用了minitest来存根,但我想在第二次和第三次使用不同的值调用它时将其存根。
我应该只使用rSpec吗?新手呼叫露比。
以下是代码片段
test "crawler does not paginate if no new items in next page" do
# 1: A, B
# 2: B, D => D
# 3: A => stop
crawler = CrawlJob.new
first_page = [
{"id"=> "item-A"},
{"id"=> "item-B"}
]
second_page = [
{"id"=> "item-B"},
{"id"=> "item-D"}
]
third_page = [{"id"=> "item-A"}]
# can only stub with same second page
# but I want to respond with third page
# the second time get_next_page is called
crawler.stub :get_next_page, second_page do
items, times_paginated = crawler.paginate_feed(first_page)
assert times_paginated == 3
end
end发布于 2015-06-20 05:25:00
我不知道Minitest,但是通过在and_return中指定多个返回值,RSpec能够在每次调用该方法时返回不同的值。
allow(crawler).to receive(:get_next_page).and_return(first_page, second_page, third_page)https://stackoverflow.com/questions/30873890
复制相似问题