我有两个规范,当单独运行时,执行得很好,但是当我使用“rspec spec”执行它们时,后者失败了。
第一的:
describe "DB Archival" do
it "should archive the nodes which are deleted" do
node_data_archival = mock()
NodeDataArchival.expects(:new).with('node1').returns(node_data_archival)
node_data_archival.expects(:export_to_csv).with([1,2])
db_archival.archive_deleted_nodes
end
end
第二:
describe "Export to CSV" do
it "should export data to a csv file" do
class School < ActiveRecord::Base
end
School.expects(:column_names).returns(["id", "name"])
node_data_archival = NodeDataArchival.new("school")
node_data_archival.export_to_csv(rows, filename)
end
end
第二个抛出:
$ rspec spec
.......F......
Failures:
1) Export to CSV should export data to a csv file
Failure/Error: node_data_archival1 = NodeDataArchival.new("school")
Mocha::ExpectationError:
unexpected invocation: NodeDataArchival.new('school')
unsatisfied expectations:
- expected exactly once, not yet invoked: School(Table doesn't exist).column_names(any_parameters)
satisfied expectations:
- expected exactly once, invoked once: NodeDataArchival.new('node1')
- expected exactly once, invoked once: #<RSpec::Mocks::Mock:0x7ff4693d3028>.export_to_csv([1, 2])
# ./spec/entity_traversal/node_data_archival_spec.rb:16:in `block (2 levels) in <top (required)>'
我的问题是 - 1)为什么它试图找到一个期望而不是在第二个规范中创建一个实际的 NodeDataArchival 实例?2)为什么第二个规范能够找到第一个规范声明的expects()?
PS:只有当我使用“rspec spec”同时运行所有规范并且两个规范单独运行良好时,才会出现问题。
编辑:使用的版本:ruby-1.9.3-p125、rspec (2.13.0) 和 mocha (0.14.0)
编辑:使用 rspec-rails-mocha (0.3.2) 而不是 mocha (0.14.0) 解决了这个问题。