简体   繁体   English

单元测试结构图提供了一个实现

[英]unit testing structuremap provides an implementation

how can I write a unit test to test that asking for an interface does provide an implementation? 我该如何编写单元测试来测试请求接口确实提供了实现? I'm using NUnit together with StructureMap 我正在使用NUnit和StructureMap

Maybe something like this based on examples in the code? 也许是基于代码示例的东西? The problem I have is that I don't know how to setup the test. 我的问题是我不知道如何设置测试。

Code snippet 程式码片段

[Test]
public void TestCustomerRegistration()
{
    var res = ObjectFactory.GetInstance<ICustomer>();

    Assert.IsNotNull(res, "Cannot get an Customer");
}

This is already present in the code 这已经存在于代码中

Not sure if I have to use a similar thing for my ICustomer test? 不确定我的ICustomer测试是否必须使用类似的方法? Not sure what this is. 不知道这是什么。

[TestFixtureSetUp]
public void SetUp()
{
    ObjectFactory.Initialize(x =>           
    {
        x.AddRegistry<InfrastructureRegistry>();
        x.AddRegistry<RepositoryRegistry>();
    });

    var container = ObjectFactory.Container;
    IDependencyResolver resolver = new SmDependencyResolver(container);
    ObjectFactory.Configure(x =>
    {
        x.For<IDependencyResolver>().Use(resolver);
    });
}

Take the folloing MVC controller 跟随下面的MVC控制器

I need to write a test that StructureMap provides 'Customer' when the _customer.GetById(id) is called. 我需要编写一个测试,以在调用_customer.GetById(id)时StructureMap提供“ Customer”。 Hope this helps 希望这可以帮助

private ICustomer _customer;

public MyController(ICustomer customer)
{
    _customer = customer;
}

public ActionResult GetCustomer(int id)
{
   var result = _customer.GetById(id);  // I need to test that _customer works
}

Thanks, 谢谢,

If you are writing useless tests for checking some configuration of StructureMap (which is used for unit-tests, not for real running application), then it's easy to do! 如果您正在编写无用的测试来检查StructureMap的某些配置(用于单元测试,而不是用于实际运行的应用程序),那么这很容易做到! Use Assert.IsInstanceOf<T> to check exact type of returned object. 使用Assert.IsInstanceOf<T>检查返回对象的确切类型。 It should be type which you are expecting StructureMap configured to return for customer interface: 它应该是您希望StructureMap配置为返回给客户界面的类型:

[Test]
public void TestCustomerRegistration()
{
    var customer = ObjectFactory.GetInstance<ICustomer>();
    Assert.IsNotNull(customer, "Cannot get an Customer");
    Assert.IsInstanceOf<SpecificCustomerType>(customer, "Wrong customer type"); 
}

UPDATE you should test controller in isolation from other objects (otherwise you will not know source of failures) and your controller test should look like (Moq sample) 更新时,您应该将控制器与其他对象隔离开来进行测试(否则您将不知道失败的原因),并且控制器测试应类似于(最小起订量示例)

[Test]
public void ShouldReturnExistingCustomer()
{
    // Arrange
    int id = 42; // or random numer
    var dto = // data which ICustomer should return
    Mock<ICustomer> customerMock = new Mock<ICustomer>();
    customerMock.Setup(c => c.GetById(id)).Returns(dto)
    var controller = new MyController(customer.Object);
    // Act
    var result = controller.GetCustomer(id);
    // Assert
    customerMock.VerifyAll();
    Assert.IsNotNull(result); // etc
}

This is a controller test and it verifies that controller works (and calls customer to get data). 这是一个控制器测试,它验证控制器是否正常工作(并致电客户以获取数据)。 If you want to verify _customer works, then you should write customer tests. 如果要验证_customer是否有效,则应编写客户测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM