4

I'm working on proving out using Dependency Injection with some numerous DI frameworks. I'm attempting to try to unit test some classes currently using Autofac as the DI container.

Let's say I have this class...

public class SaveUserCommand : DBCommandBase<UserImpl>
{
    public delegate SaveUserCommand Factory(UserImpl impl);

    private UserImpl impl;
    private IAuditableHelper helper;

    public SaveUserCommand(UserImpl impl, IAuditableHelper helper)
    {
        this.impl = impl;
        this.helper = helper;
    }

    public override UserImpl Execute(object dataTrans)
    {
        return this.impl;
    }
}

^Command structured business layer btw.

I have another command that relies on the above command in this way...

public class SaveSpecialUserCommand : DBCommandBase<UserImpl>
{
    public delegate SaveSpecialUserCommand Factory(UserImpl user);

    private UserImpl user;
    SaveUserCommand.Factory saveUserCommand;

    public SaveSpecialUserCommand(UserImpl user, SaveUserCommand.Factory saveUserCommand)
    {
        this.user = user;
        this.saveUserCommand = saveUserCommand;
    }

    public override UserImpl Execute(object dataTrans)
    {
        this.user.IsSpecial = true;
        this.saveUserCommand(this.user).Execute(dataTrans);
        return this.user;
    }
}

Using Autofac, it resolves all dependencies in the SaveSpecialUserCommand.

What I am unsure of, is how I can unit test or inject a mock into the SaveUserCommand.Factory delegate.

Hints would be good. I still want to figure this out, but a general direction would be awesome.

EDIT

Just adding a simple test case showing I do not want to use Autofac in my unit tests to create my commands.

    [Test]
    public void SomeSimpleTestTest()
    {
        var user = new UserImpl();

        var command = new SaveSpecialUserCommand(user, /*This is what I need to mock. SaveUserCommand.Factory*/null);
        var retVal = command.Execute(this._mockTransaction);

        Assert.IsNotNull(retVal);
        Assert.IsTrue(retVal.IsSpecial);
    }

1 Answer 1

5

If you resolve SaveSpecialUserCommand through the container, you can't mock the factory delegate since this is a piece that Autofac autogenerates for you. The question is then, why do you need to fake the actual delegate?

Update: bit of misunderstanding initially there. To "fake" a delegate you can simply use a lambda, like this:

var user = new UserImpl();
var cmd = new SaveUserCommand(...);

var command = new SaveSpecialUserCommand(user, u => cmd);
Sign up to request clarification or add additional context in comments.

3 Comments

Well, I wasn't planning on having Autofac create my commands for unit tests. I'll edit with the test case that I was attempting to work with.
Hmm...Unless you are suggesting I use my DI container in my unit tests, and place mocks inside the container for test cases?
No, I was under the impression that you did use a container in your unit test. See my updated answer then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.