我使用一个SupplierController
类及其SupplierControllerTest
类来验证我的期望。
如果我的SupplierController
类继承自 System.Web.Mvc.Controller 则测试运行正常。如果我的SupplierController
类继承自然ServiceStack.Mvc.ServiceStackController
后测试抛出异常。我正在使用Moq来测试它。
这是两个类:
测试班
[TestFixture]
public class SupplierControllerTests
{
[Test]
public void Should_call_create_view_on_view_action()
{
var nafCodeServiceMock = new Mock<INafCodeService>();
var countryServiceMock = new Mock<ICountryService>();
var controller = new SupplierController();
controller.NafCodeService = nafCodeServiceMock.Object;
controller.CountryService = countryServiceMock.Object;
nafCodeServiceMock.Setup(p => p.GetAll()).Returns(new List<NafCode> { new NafCode { Code = "8853Z", Description = "naf code test" } });
countryServiceMock.Setup(p => p.GetAll()).Returns(new List<Country> { new Country { Name="France" } });
var result = controller.Create() as ViewResult;
Assert.That(result, Is.Not.Null);
}
}
控制器类
public class SupplierController : ServiceStackController
{
public ISupplierService SupplierService { get; set; }
public IManagerService ManagerService { get; set; }
public INafCodeService NafCodeService { get; set; }
public ICountryService CountryService { get; set; }
public ActionResult Create()
{
var model = new SupplierModel();
model.Country = "France";
return View(model);
}
}
供应商模型类
public class SupplierModel
{
public string Country { get; set; }
}
抛出的错误是:
测试 'SupplierControllerTests.Should_call_create_view_on_view_action' 失败:System.MethodAccessException : Échec de la tentative d'accès de la 方法 'SupplierController.Create()' à la 方法 'System.Web.Mvc.Controller.View(System.Object)'。Controllers\SupplierController.cs(51,0): à SupplierController.Create() Controllers\SupplierControllerTests.cs(33,0): à SupplierControllerTests.Should_call_create_view_on_view_action()
翻译这意味着:
访问方法“SupplierController.Create()”失败。