什么是犀牛嘲笑的意思"要求返回值或异常抛出"? [英] What does Rhino Mocks mean by "requires a return value or an exception to throw"?

查看:199
本文介绍了什么是犀牛嘲笑的意思"要求返回值或异常抛出"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在嘲讽到WCF服务的调用,我得到以下错误:




方法ICustomerEntities.GetCustomerFromPhoneNumber(01234123123) ;'需要一个返回值或异常抛出




我GOOGLE了这一点,并在这里搜查 - 所有我能找到的是我需要重新整理各种调用等等,但是这似乎并没有什么意义,我的情况?也许有人会指出,我认为它实际上确实的?



我的测试设置是这样的。



  _entities = MockRepository.GenerateStrictMock< ICustomerEntities>(); 

和测试方法失败在第三行,设置 RESULT2

  _entities.Expect(IP = GT; ip.GetCustomerFromPhoneNumber(01234123123)); 
VAR测试= MockRepository.GenerateMock< ICustomerEntities>(_厂);
VAR结果2 = _entities.GetCustomerFromPhoneNumber(01234123123);
VAR的结果= test.GetAllCustomersWithAGivenPhoneNumber(01234123123);
Assert.AreEqual(结果,结果2);



原来的呼叫试图嘲笑这个(它在一个名为方法 GetAllCustomersWithAGivenPhoneNumber ):

 使用(VAR实体= _factory.CreateEntities())
$ { b $ b VAR的客户= entities.GetCustomerFromPhoneNumber(telephoneNumber);
}


解决方案

记住一个嘲笑对象就是如此,一个模拟。有没有实现可言,因此,如果模拟需要从一个函数返回一个值,它没有知道该怎么回报,除非你告诉它的方式。



我假设你GetCustomerFromPhoneNumber()返回一个Customer对象或id,所以你需要告诉犀牛什么返回:



我从来没有用过犀牛,但希望这将指向你在即使语法是不完全正确方向是正确的。

  VAR测试= MockRepository.GenerateMock< ICustomerEntities>(_厂) ; 
test.Stub(ENT => ent.GetCustomerFromPhoneNumber(01234123123))。返回(新客户());


When mocking a call to a WCF Service, I get the following error:

Method 'ICustomerEntities.GetCustomerFromPhoneNumber("01234123123");' requires a return value or an exception to throw.

I've googled this and searched on here - all I can find is I need to re-order various calls etc, but that doesn't seem to make sense in my situation? Perhaps someone could point out to me that it actually does?

My test setup looks like this

_entities = MockRepository.GenerateStrictMock<ICustomerEntities>();

And the test method fails on the third line, setting result2

_entities.Expect(ip => ip.GetCustomerFromPhoneNumber("01234123123"));
var test = MockRepository.GenerateMock<ICustomerEntities>(_factory);
var result2 = _entities.GetCustomerFromPhoneNumber("01234123123");
var result = test.GetAllCustomersWithAGivenPhoneNumber("01234123123");
Assert.AreEqual(result,result2);

The original call is attempting to mock this (its in a method called GetAllCustomersWithAGivenPhoneNumber):

using (var entities = _factory.CreateEntities())
{
    var customer = entities.GetCustomerFromPhoneNumber(telephoneNumber);
}

解决方案

Remember a mocked object is just that, a mock. There is no implementation at all, so if the mock needs to return a value from a function, it has no way of knowing what to return unless you tell it.

I assume your GetCustomerFromPhoneNumber() returns a Customer object or id, so you need to tell Rhino what to return:

I've never used Rhino, but hopefully this will point you in the right direction even if the syntax isn't quite right.

var test = MockRepository.GenerateMock<ICustomerEntities>(_factory);
test.Stub(ent => ent.GetCustomerFromPhoneNumber("01234123123")).Return(new Customer());

这篇关于什么是犀牛嘲笑的意思&QUOT;要求返回值或异常抛出&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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