C# 如何测试ServiceStackController?
我使用C# 如何测试ServiceStackController?,c#,asp.net-mvc,mocking,
servicestack,moq,C#,Asp.net Mvc,Mocking,
servicestack,Moq,我使用SupplierController类及其SupplierControllerTest类来验证我的期望 如果mySupplierController类继承自System.Web.Mvc.Controller,则测试运行正常。 如果mySupplierController类继承自ServiceStack.Mvc.ServiceStackController,则测试会引发异常。 我正在使用Moq来测试它 以下是两个课程: 测试类 [TestFixture] public class S
SupplierController
类及其SupplierControllerTest
类来验证我的期望
如果mySupplierController
类继承自System.Web.Mvc.Controller,则测试运行正常。
如果mySupplierController
类继承自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; }
}
供应商模型类
[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.Bould_call_create_view_on_view_action”失败:System.MethodAccessException:èchec de la暂定d'accès de la méthode'SupplierController.create()“la méthode”System.Web.Mvc.Controller.view(System.Object)”。
Controllers\SupplierController.cs(51,0):a SupplierController.Create()
Controllers\SupplierControllerTests.cs(33,0):供应商controllertests
这意味着:
访问方法“SupplierController.Create()”失败
我要试试看。我不知道您使用的是哪个版本的MVC,但我猜ServiceStack是根据旧版本编译的。您将需要绑定重定向。现在,绑定重定向通常作为MVC项目模板的一部分添加,但在单元测试项目中,您必须手动执行(这解释了为什么您只在单元测试中遇到此错误) 在单元测试项目app.config中(此示例将从MVC2重定向到MVC3。根据您的情况进行调整):
我要试试看。我不知道您使用的是哪个版本的MVC,但我猜ServiceStack是根据旧版本编译的。您将需要绑定重定向。现在,绑定重定向通常作为MVC项目模板的一部分添加,但在单元测试项目中,您必须手动执行(这解释了为什么您只在单元测试中遇到此错误) 在单元测试项目app.config中(此示例将从MVC2重定向到MVC3。根据您的情况进行调整):
任何地方有内部类吗?ToEditModel类是内部类吗?在您的好评后添加了SupplierModel类。有内部类吗?ToEditModel类是内部的吗?在您的好评之后添加了SupplierModel类。