Tag: 嘲笑

什么是Java中的模拟对象?

我想知道Java中的模拟对象 。 为什么我们要创造它们,它们的用途是什么?

在PHPUnit中,如何在连续调用模拟方法时指出与()不同?

我想用不同的预期参数两次调用我的模拟方法。 这是行不通的,因为expects($this->once())在第二次调用时会失败。 $mock->expects($this->once()) ->method('foo') ->with('someValue'); $mock->expects($this->once()) ->method('foo') ->with('anotherValue'); $mock->foo('someValue'); $mock->foo('anotherValue'); 我也试过了: $mock->expects($this->exactly(2)) ->method('foo') ->with('someValue'); 但是,如何添加一个with()来匹配第二个调用呢?

在包装函数之前,我可以修补Python装饰器吗?

我有一个装饰器的function,我试图在Python 模拟库的帮助下进行testing。 我想使用mock.patch来replace真正的装饰器,只是调用函数的模拟“旁路”装饰器。 我无法弄清楚的是如何在真正的装饰器封装函数之前应用这个补丁。 我已经尝试了修补程序目标上的一些不同的变体,并重新sorting修补程序和导入语句,但没有成功。 有任何想法吗?

什么是对象嘲笑,我什么时候需要它?

许多人在编写unit testing时使用模拟对象。 什么是模拟对象 ? 为什么我会需要一个? 我需要一个模拟对象框架吗?

PHPUnit:我如何模拟多个方法调用多个参数?

我正在为使用PHPUnit的方法编写unit testing。 我正在testing的方法在同一个对象上调用同一个方法3次,但使用不同的参数集。 我的问题类似于这里和这里提出的问题 在其他文章中提出的问题与嘲笑只有一个参数的方法有关。 但是,我的方法需要多个参数,我需要这样的东西: $mock->expects($this->exactly(3)) ->method('MyMockedMethod') ->with($this->logicalOr($this->equalTo($arg1, $arg2, arg3….argNb), $this->equalTo($arg1b, $arg2b, arg3b….argNb), $this->equalTo($arg1c, $arg2c, arg3c….argNc) )) 此代码不起作用,因为equalTo()只validation一个参数。 给它多个参数会引发一个exception: PHPUnit_Framework_Constraint_IsEqual :: __ construct()的参数#2必须是数字 有没有办法做logicalOr嘲笑一个方法有多个参数? 提前致谢。

模拟或存根链接调用

protected int parseExpire(CacheContext ctx) throws AttributeDefineException { Method targetMethod = ctx.getTargetMethod(); CacheEnable cacheEnable = targetMethod.getAnnotation(CacheEnable.class); ExpireExpr cacheExpire = targetMethod.getAnnotation(ExpireExpr.class); // check for duplicate setting if (cacheEnable.expire() != CacheAttribute.DO_NOT_EXPIRE && cacheExpire != null) { throw new AttributeDefineException("expire are defined both in @CacheEnable and @ExpireExpr"); } // expire time defined in @CacheEnable or @ExpireExpr return cacheEnable.expire() != […]

嘲笑Java InputStream

请指点一下,帮我嘲笑java的InputStream对象。 这是我希望模拟的代码行: InputStreamReader inputData = new InputStreamReader(System.in); bufferdReader = new BufferedReader(inputData); bufferdReader.readLine();

使用模拟修补程序来模拟实例方法

我试图嘲笑一些东西,而使用富有想象力的模拟testing库来testing一个Django应用程序。 我似乎无法完成它的工作,我试图这样做: models.py from somelib import FooClass class Promotion(models.Model): foo = models.ForeignKey(FooClass) def bar(self): print "Do something I don't want!" test.py class ViewsDoSomething(TestCase): view = 'my_app.views.do_something' def test_enter_promotion(self): @patch.object(my_app.models.FooClass, 'bar') def fake_bar(self, mock_my_method): print "Do something I want!" return True self.client.get(reverse(view)) 我究竟做错了什么?

嘲笑使用python模拟function

我想模拟一个函数(返回一些外部内容)使用python模拟模块(http://www.voidspace.org.uk/python/mock/index.html)。 我有一些麻烦嘲笑导入到模块的function。 例如,在util.py我有 def get_content(): return "stuff" 我想嘲笑util.get_content,以便它返回别的东西。 我正在尝试这个: util.get_content=Mock(return_value="mocked stuff") 如果get_content被另一个模块调用,它实际上似乎不会返回模拟对象。 我错过了如何使用模拟的东西? 请注意,如果我调用以下,事情正常工作: >>> util.get_content=Mock(return_value="mocked stuff") >>> util.get_content() "mocked stuff" 但是,如果从另一个模块内部调用get_content,则会调用原始函数而不是模拟版本: >>> from mymodule import MyObj >>> util.get_content=Mock(return_value="mocked stuff") >>> m=MyObj() >>> m.func() "stuff" mymodule.py的内容 from util import get_content class MyObj: def func(): get_content() 所以我想我的问题是 – 我怎么才能从我调用的模块内调用函数的Mocked版本? 看起来, from module import function可能是在这里指责,因为它不指向模拟function。

EasyMock:如何在没有警告的情况下创buildgenerics类的模拟?

代码 private SomeClass<Integer> someClass; someClass = EasyMock.createMock(SomeClass.class); 给我一个警告“types安全性:SomeClasstypes的expression式需要未经检查的转换符合SomeClass <Integer>”。