Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: setup nested dependencies mocks with AutoMoqCustomization #608

Closed
Eldar1205 opened this issue Apr 15, 2016 · 5 comments
Closed

Comments

@Eldar1205
Copy link

I've tried using the AutoMoqCustomizationto auto-mock an object graph with nested dependencies and results are not as I expected. Here's the test code, can someone please tell me why after freezing a Mock<IObject> there's a situation where another IObjectcreated by Moq is created by the fixture?

[TestClass]
    public class MyTestClass
    {
        [TestMethod]
        public void Test()
        {
            var fixture = new Fixture().Customize(new AutoMoqCustomization());

            var objMock = fixture.Freeze<Mock<IObject>>();
            var sut = fixture.Create<Dependant2>();

            sut.Obj.Should().BeSameAs(objMock);
        }
    }

    public interface IObject { }

    public interface IDependant { IObject Obj { get; } }

    public class MyObject : IObject { }

    public class Dependant2
    {
        public Dependant2(IDependant dependant)
        {
            Obj = dependant.Obj;
        }

        public IObject Obj { get; }
    }
@dcastro
Copy link
Contributor

dcastro commented Apr 15, 2016

The AutoMoqCustomization creates mock objects but does not set them up.

If you want the mock's members to be set up, consider using the AutoConfiguredMoqCustomization instead. It'll a) set the mock's public settable properties and b) setup methods/indexers to return (and memoize) values lazily generated by the fixture

There are two limitations. It doesn't setup:

  • methods with ref parameters
  • generic methods

More info at the bottom of the cheat sheet.

@Eldar1205
Copy link
Author

Thanks for fast response! I tried AutoConfiguredMoqCustomization with method instead of property in IDependant and it respected the frozen instance. Is there a way to make non-settable properties to be respected as well? With Moq one can setup a property getter even when there's no setter.

@dcastro
Copy link
Contributor

dcastro commented Apr 15, 2016

Actually, nevermind. It does setup get-only properties too, my bad.

I also remember a bug in Moq that interfered with AutoMoq, making it unable to setup get-only properties. At least back when I looked into this, you had to use Moq version 4.2.1409.1722 or lower (details at #434). Maybe the bug has been fixed in newer version of Moq, I'm not sure.

@Eldar1205
Copy link
Author

Tried the highest Moq version that is lower than 4.2.1409.1722 and you're correct, it works for get-only properties as well. The Moq bug wasn't fixed in a newer version, since I tried it with latest. Thank you very much for your help

@ploeh
Copy link
Member

ploeh commented Apr 16, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants