我正在嘗試爲inherited_resources控制器編寫規範。我決定使用rspec的mock_model模擬與數據庫的所有集成。不幸的是,我不能編寫創建和更新操作的規範,因爲我得到以下錯誤:https://gist.github.com/936947 有人可以幫我解決這個問題嗎?RSpec mock_model和inherited_resources
3
A
回答
4
我在使用flexmock時遇到同樣的問題。
原因是它沒有使用update_attributes
方法來做出路由決定。它檢查resource.errors
以查看它是否爲空。
所以爲了讓它正確響應,我們還需要剔除errors
方法。
下面是相關代碼@line 248的lib/inherited_resources/base_helpers.rb
def respond_with_dual_blocks(object, options, &block) #:nodoc:
args = (with_chain(object) << options)
case block.try(:arity)
when 2
respond_with(*args) do |responder|
blank_slate = InheritedResources::BlankSlate.new
if object.errors.empty?
block.call(responder, blank_slate)
else
block.call(blank_slate, responder)
end
end
when 1
respond_with(*args, &block)
else
options[:location] = block.call if block
respond_with(*args)
end
end
0
失敗消息是關於無法從控制器內部訪問命名路由,所以我不確定這與mock_model有什麼關係。你是否用真實模型試過相同的例子?
+0
有了真正的機型它的工作沒有任何問題。 – luacassus 2011-04-22 19:47:43
它解決了我的問題。非常感謝! ;) – luacassus 2011-04-29 06:41:21
用什麼來模擬出錯誤方法?我遇到了這個完全相同的問題...... – sevenseacat 2011-09-04 14:28:05
啊,對了,我用'team.stub_chain(:errors,:empty?)。和_return(false)'來使用stub_chain,並且它完美地工作。 – sevenseacat 2011-09-04 14:38:46