سؤال

I have been using RhinoAutoMocker for unit testing, and it works very well in almost all cases. I'm currently having trouble figuring out is how to use it when the Class Under Test has primitive constructor arguments.

Let's say I have two classes defined as such:

public class AddAnswerValidator : BaseValidator
{
    public AddAnswerValidator(Answer answerToAdd, 
                              IAnswerRepository answerRepository)
    {
        ...some code
    }

    public override bool Validates()
    {
        ...some code
    }
}

public class RemoveAnswerValidator : BaseValidator
{
    public RemoveAnswerValidator(int answerIDToRemove, 
                                 IAnswerRepository answerRepository)
    {
        ...some code
    }

    public override bool Validates()
    {
        ...some code
    }
}

An example test for each are as follows:

[Test]
public void AddAnswerValidatorTests_Validates_ValidInput_ReturnsTrue()
{
    var autoMocker = new RhinoAutoMocker<AddAnswerValidator>();
    var fakeAnswer = new Answer();
    autoMocker.Inject<Answer>(fakeAnswer);

    var result = autoMocker.ClassUnderTest.Validates();

    Assert.IsTrue(result);
}

[Test]
public void RemoveAnswerValidatorTests_Validates_ValidInput_ReturnsTrue()
{
    var autoMocker = new RhinoAutoMocker<RemoveAnswerValidator>();
    var fakeAnswerID = 1;
    autoMocker.Inject<int>(fakeAnswerID);

    var result = autoMocker.ClassUnderTest.Validates();

    Assert.IsTrue(result);
}

The first test (for AddAnswerValidator) works fine. The second test (for RemoveAnswerValidator) fails with a StructureMap 202 Error "No default instance defined for plugin family RemoveAnswerValidator" error. I'm working under the assumption that the second test is failing because StructureMap isn't resolving the integer constructor argument.

I've read through this post regarding RhinoAutoMocker Injection for collections and I've been tracing through the source code on GitHub, but I don't understand why the primitive value isn't being injected.

I've even tried substituting some of the overloaded Inject methods available on the container object such as:

autoMocker.Inject<int>(fakeAnswerID);

with

autoMocker.Container.Inject<int>("answerIDToRemove", fakeAnswerID);

but using the name of the constructor argument doesn't produce any different results.

--

In the long run, this isn't a huge problem since I can always just create an instance of the Class Under Test and create my own mocks, it would just be nice to be able to use a consistent methodology across all of my tests.

هل كانت مفيدة؟

المحلول

I know, it's a little bit too late, but i had the same problem and managed to solve it with integer parameter:

var autoMocker = new RhinoAutoMocker<RemoveAnswerValidator>();
automocker.Container.Configure(c=>
         c.For<RemoveAnswerValidator>()
           .Use<RemoveAnswerValidator>()
                .Ctor<int>()
                .Is(1));

نصائح أخرى

While I never did find a way to inject a primitive using AutoMocker, I ended up working around the problem by creating a parameter object and injecting that instead.

The parameter object then includes the necessary primitives as a properties, in addition to the other (former) parameters. The example above would be changed to:

public class RemoveAnswerValidator : BaseValidator
{
    public RemoveAnswerValidator(RemoveAnswerValidatorParameters parms)
    {
        ...some code
    }

    public override bool Validates()
    {
        ...some code
    }
}

public class RemoveAnswerValidatorParameters
{
    public int AnswerID { get; set; }
    public IAnswerRepository AnswerRepository { get; set; }
}

(Then in the test class)

[Test]
public void RemoveAnswerValidatorTests_Validates_ValidInput_ReturnsTrue()
{
    var autoMocker = new RhinoAutoMocker<RemoveAnswerValidator>();
    var fakeAnswerParameters = new FakeAnswerParameters()
    {
        AnswerID = 1,
        AnswerRepository = autoMocker.Get<IAnswerRepository>()
    };
    autoMocker.Inject<RemoveAnswerValidatorParameters>(fakeAnswer);

    var result = autoMocker.ClassUnderTest.Validates();

    Assert.IsTrue(result);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top