可以使用默认属性值创建模拟 [英] Mocks can be created with default attribute values

查看:35
本文介绍了可以使用默认属性值创建模拟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用默认属性值创建模拟吗?我不确定这个问题的真正含义.两种可能:

can mocks be created with default attribute values? I am not certain what this question really mean. Two possibilities:

  1. 如果一个对象t=Movie.find(10),然后如果我说m=mock(t),m会自动继承所有的t 的属性值?

  1. if an object t=Movie.find(10), and then if I say m=mock(t), does m automatically inherit all the attribute values of t?

有没有办法创建一个具有默认属性的模拟对象,这样我们每次使用时都可以克隆它,而无需通过系列存根来初始化"这些属性?>

is there a way to create a mock object with default attributes such that we can clone it everytime when we use it without going thru series stubs to "initialize" those attributes?

感谢您的帮助.

推荐答案

说到 1) 没有意义.如果在测试中您可以通过 Movie.find(10) 访问真实对象,则不必使用模拟.假设您正在使用 rspec_mocks 并且您要编写一个断言该特定方法被调用,您可以简单地编写:

When it comes to 1) It does not have a sense. If inside the test you have an access to the real object through Movie.find(10) you don't have to use mocks. Assuming that you're using rspec_mocks and you're going to write an assertion that particular method was called, you could simply write:

t = Movie.find(10)
t.should_receive(:foo)

另请查看:https://www.relishapp.com/rspec/rspec-rails/v/2-11/docs/mocks/mock-model而这个 https://www.relishapp.com/rspec/rspec-rails/v/2-11/docs/mocks/stub-model

在规范中模拟 AR/Mongoid 和其他模型的非常有用的助手.

Very useful helpers for mocking AR/Mongoid and other models in specs.

factory_girl 一起,您可以在此模拟上设置属性,例如 movie = mock_model(Movie, FactoryGirl.attributes_for(:move))

Along with factory_girl you could set an attributes on this mocks, for example movie = mock_model(Movie, FactoryGirl.attributes_for(:move))

这篇关于可以使用默认属性值创建模拟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆