我有一个名为 get_books_from_amazon 的辅助方法,它执行 API 调用并返回一组书籍。我无法弄清楚如何在我的请求规范中将其存根。
module BooksHelper
def get_books_from_amazon(search_term)
...
end
end
class StaticController < ApplicationController
include BooksHelper
def resources
@books = get_books_from_amazon(search_term)
end
end
我在规范中尝试了以下方法,但均无济于事:
# spec/requests/resource_pages_spec.rb
...
describe "Navigation" do
it "should do such and such" do
BooksHelper.stub!(:get_books_from_amazon).and_return(book_array)
StaticHelper.stub!(:get_books_from_amazon).and_return(book_array)
ApplicationHelper.stub!(:get_books_from_amazon).and_return(book_array)
StaticController.stub!(:get_books_from_amazon).and_return(book_array[0..4])
ApplicationController.stub!(:get_books_from_amazon).and_return(book_array[0..4])
request.stub!(:get_books_from_amazon).and_return(book_array)
helper.stub!(:get_books_from_amazon).and_return(book_array)
controller.stub!(:get_books_from_amazon).and_return(book_array)
self.stub!(:get_books_from_amazon).and_return(book_array)
stub!(:get_books_from_amazon).and_return(book_array)
visit resources_path
save_and_open_page
end
关于问题是什么的任何想法?