0

Hello I have code like below and use xUnit. I would like to write TestClass to test my interface. Could you tell my how can I:

  • inject different services to test class by DependencyInjection and run test for this services.
  • prepare object to inject with Autofixture and AutoMoq. Before inject i would like to create services like

I want to do somethink like this :

public ServiceTestClass
{
    private ISampleService sut;
    public ServiceTestClass(ISampleService service) {
        this.sut = service;
    }

    [Fact]
    public MyTestMetod() {
        // arrange
        var fixture = new Fixture();

        // act
        sut.MakeOrder();  

        // assert
        Assert(somethink);
    }
}


public class SampleService : ISampleService // ande few services which implements ISampleService
{
    // ISampleUow also include few IRepository
    private readonly ISampleUow uow;
    public SampleService(ISampleUow uow) {
        this.uow = uow;
    }

    public void  MakeOrder() {
        //implementation which use uow
    }
}

1 Answers1

3

It isn't particularly clear what you're asking, but AutoFixture can serve as an Auto-mocking Container. The AutoMoq Glue Library is one of many extensions that enable AutoFixture to do that. Others are AutoNSubstitute, AutoFakeItEasy, etc.

This enables you to write tests like this one:

[Fact]
public void MyTest()
{
    var fixture = new Fixture().Customize(new AutoMoqCustomization());
    var mock = fixture.Freeze<Mock<ISampleUow>>();
    var sut = fixture.Create<SampleService>();

    sut.MakeOrder();

    mock.Verify(uow => /* assertion expression goes here */);
}

If you combine it with the AutoFixture.Xunit2 Glue Library, you can condense it to this:

[Theory, MyAutoData]
public void MyTest([Frozen]Mock<ISampleUow> mock, SampleService sut)
{
    var sut = fixture.Create<SampleService>();    
    sut.MakeOrder();    
    mock.Verify(uow => /* assertion expression goes here */);
}
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • Thank You for answer but you didn't understand me. First I want to call my ServiceTestClass constructor with ISampleServiceArgument and run tests. Second u would like to use fixture.Create with ISampleService - not SampleService. – user2811005 Sep 17 '15 at 06:20
  • 1
    @user2811005 As given in the OP, `ServiceTestClass` doesn't have a constructor that takes `ISampleService` as an argument. Even if it had, AFAICT, it's a test class, which means that it's whatever *test runner* you use that creates instances of that class. Unless you go out of your way, *xUnit.net* expects that class to have a parameterless constructor (which it currently has). – Mark Seemann Sep 17 '15 at 06:52
  • I have done mistake in my sample code -fogotten about constructor. I have just fixed it. But it doesn't matter. So, to sum up - as you said It doesn;t possible to create constructor with parametr in TestClass ? – user2811005 Sep 17 '15 at 12:30
  • @user2811005 It's a question if *xUnit.net* contains the extensibility points for that. IIRC, *xUnit.net 1* had a hook that could enable you to do something like that, but it was a lot of trouble, so not practical. I haven't looked into *xUnit.net 2* enough to know whether or not that's possible. – Mark Seemann Sep 17 '15 at 12:35
  • 1
    @user2811005 In any case, *why* do you want to do this? That's not a normal thing to do, so I wonder if there's a usage scenario I haven't seen before... – Mark Seemann Sep 17 '15 at 12:36