Ruby on rails RSpec中mock和mock_模型的区别是什么
我最近遇到了不同的教程,人们同时使用Ruby on rails RSpec中mock和mock_模型的区别是什么,ruby-on-rails,ruby,testing,rspec,Ruby On Rails,Ruby,Testing,Rspec,我最近遇到了不同的教程,人们同时使用mock和mock\u model函数 在中,他们使用mock\u model功能,但在中,只有mock功能,但没有mock\u model 我试着自己运行一些测试,但没有发现任何真正的区别,因为当我使用这两个函数中的任何一个时,一切都正常,所以有什么区别吗?来自: 首先,mock\u模型自动 定义模型的唯一ID,该ID 是用它创建的。第二,它 将方法定义为_param(返回 id的字符串表示形式)和 新记录?(返回false) 正如jenger所说,mock
mock
和mock\u model
函数
在中,他们使用mock\u model
功能,但在中,只有mock
功能,但没有mock\u model
我试着自己运行一些测试,但没有发现任何真正的区别,因为当我使用这两个函数中的任何一个时,一切都正常,所以有什么区别吗?来自:
首先,mock\u模型
自动
定义模型的唯一ID,该ID
是用它创建的。第二,它
将方法定义为_param
(返回
id的字符串表示形式)和
新记录?
(返回false)
正如jenger所说,mock_模型是为活动记录构建的扩展: 这是1.2.6中的来源:
def mock_model(model_class, options_and_stubs = {})
id = options_and_stubs[:id] || next_id
options_and_stubs = options_and_stubs.reverse_merge({
:id => id,
:to_param => id.to_s,
:new_record? => false,
:errors => stub("errors", :count => 0)
})
m = mock("#{model_class.name}_#{id}", options_and_stubs)
m.__send__(:__mock_proxy).instance_eval <<-CODE
def @target.as_new_record
self.stub!(:id).and_return nil
self.stub!(:to_param).and_return nil
self.stub!(:new_record?).and_return true
self
end
def @target.is_a?(other)
#{model_class}.ancestors.include?(other)
end
def @target.kind_of?(other)
#{model_class}.ancestors.include?(other)
end
def @target.instance_of?(other)
other == #{model_class}
end
def @target.class
#{model_class}
end
CODE
yield m if block_given?
m
end
def mock_模型(模型_类、选项_和_存根={})
id=选项和存根[:id]| |下一个_id
选项\u和\u存根=选项\u和\u存根。反向\u合并({
:id=>id,
:to_param=>id.to_s,
:新记录?=>错误,
:errors=>stub(“errors”,:count=>0)
})
m=mock(“#{model_class.name}#{id}”,选项_和_存根)
m、 _uuusend_uuuu(:uuuu mock_proxy)。instance_ueval对于在Google中找到此问题“rspec mock_model”排名的人,可以在