Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoConfiguredMoqCustomization with abstract class implementing interface

I use AutoFixture 3.21.0, AutoFixture.AutoMoq 3.21.0, NUnit 2.6.3 and Moq 4.2.1409.1722.

I have the following interface, two abstract classes (one of them implements this interface), and two unit tests.

Tests pass.

    public interface IMigration
    {
        IMigrationParameters MigrationParameters { get; set; }
    }

    public abstract class AbstractSutWithoutInterface
    {
        public IMigrationParameters MigrationParameters { get; set; }
    }

    public abstract class AbstractSutWithInterface : IMigration
    {
        public IMigrationParameters MigrationParameters { get; set; }
    }

    [TestFixture]
    public class UnitTests
    {
        [Test]
        public void TestAbstractSutWithoutInterface()
        {
            var fixture = new Fixture();
            fixture.Customize( new AutoConfiguredMoqCustomization() );

            var mock = fixture.Create<AbstractSutWithoutInterface>();

            Assert.IsNotNull( mock.MigrationParameters ); // test passes
        }

        [Test]
        public void TestAbstractSutWithInterface()
        {
            var fixture = new Fixture();
            fixture.Customize( new AutoConfiguredMoqCustomization() );

            var mock = fixture.Create<AbstractSutWithInterface>();

            Assert.IsNull( mock.MigrationParameters ); // test passes
        }
    }

My question is why AutoConfiguredMoqCustomization has different behavior for abstract classes depending on whether property is defined by interface or not? In first test property is asserted to be not null but in second test is null. If classes are not abstract, property injection works as expected for both classes.

like image 813
sgnsajgon Avatar asked Oct 23 '14 08:10

sgnsajgon


1 Answers

Update 2015/04/15

This bug has been fixed in AutoFixture.AutoMoq 3.24.2. See details here.

Update 2014/11/03

This is now being tracked on AutoFixture's GitHub, Issue 324.

Also, the last working version of Moq is 4.2.1402.2112, you can downgrade to that one instead of 4.0.


I can only reproduce this with the latest version of Moq (4.2.1409.1722).

I'm looking into this right now, and it seeeeems like a bug has been introduced in the latest version of Moq, but it might be by design, I'm not sure yet.

In the meantime, please use version 4.0.10827. To downgrade, go to Tools -> NuGet Packet Manager -> Package Manager Console and type:

Uninstall-Package Moq -Force
Install-Package Moq -Version 4.0.10827

I'll update this answer with my findings.

like image 74
dcastro Avatar answered Sep 28 '22 09:09

dcastro