【发布时间】:2014-10-03 06:12:30
【问题描述】:
这个简单的测试有问题:
book_spec.rb
let(:books_controller) { BooksController.new }
context "GET #index" do
it "calls Book.all" do
Book.should_receive(:all)
book_controller.index
end
end
books_controller.rb
def index
respond_with books
end
private
def books
@books ||= Book.all
end
我想删除对 ActiveRecord 的调用,这样它就不需要与数据库交互并加快测试速度。但是,我似乎无法做到这一点。
编辑:
抱歉忘记了最重要的一点!
Failure/Error: book_controller.index
NoMethodError:
undefined method `variant' for nil:NilClass
【问题讨论】:
-
有什么问题?您收到错误、失败的测试或测试触发对 DB 的请求?
-
@gotva 抱歉,包含错误消息。
-
hmmm... Google 返回了很多建议。我认为this one 与您的问题非常相似,主要思想是
You can't test controllers like this due to how the Rails application life cycle works.。在那里写“经典”控制器规范和存根Book.all怎么样。
标签: ruby-on-rails rspec mocking