【发布时间】:2011-02-03 14:15:32
【问题描述】:
我正在尝试测试该属性是否从服务调用的返回中获取它的值,但是我在模拟服务调用时遇到了麻烦。
这是我的财产:
public ICountry Country
{
get
{
if (_country == null)
{
ICountryService countryService = new CountryService();
_country = countryService.GetCountryForCountryId(_address.CountryId);
}
return _country;
}
}
这是我的测试尝试:
[TestMethod]
public void Country_should_return_Country_from_CountryService()
{
const string countryId = "US";
_address.CountryId = countryId;
var address = MockRepository.GenerateStub<Address>(_address);
var country = MockRepository.GenerateMock<ICountry>();
var countryService = MockRepository.GenerateStub<ICountryService>();
countryService.Stub(x => x.GetCountryForCountryId(countryId)).IgnoreArguments().Return(country);
Assert.AreEqual(address.Country, country);
}
我不断收到错误,因为正在调用真正的 countryService,而不是我的模拟。我正在使用 MsTest 和 Rhino Mocks。我究竟做错了什么?
【问题讨论】: