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

Bug: Mocks of factories with parameters behave as if you use "It.IsAny(x)" if you mock a function call in the returned object. #147

Closed
christopherbauer opened this issue Jan 2, 2015 · 2 comments

Comments

@christopherbauer
Copy link

christopherbauer commented Jan 2, 2015

Per this post on stack overflow: http://stackoverflow.com/questions/27723216/is-moq-mocking-a-subinterface-return-value-and-ignoring-the-intermediary-step-a, when wrapping a mocked object as this:

itemServiceFactory.Setup(factory => factory.Create(true).GetItem()).Returns(anItem);

The true is ignored and behaves like an It.IsAny<bool>().

using System;
using Moq;

namespace MoqBugMCV
{
    public interface IItemServiceFactory
    {
        IItemService Create(bool shouldCreateServiceA);
    }

    public class Item
    {
        public string Name { get; set; }
        public decimal Price { get; set; }
    }

    public interface IItemService
    {
        Item GetItem();
    }

    public class ItemManager
    {
        private readonly IItemService _itemService;

        public ItemManager(IItemServiceFactory itemServiceFactory)
        {
            _itemService = itemServiceFactory.Create(true); //<==== configured true (like by app.config at runtime or something)
        }

        public Item GetAnItem()
        {
            return _itemService.GetItem();
        }
    }

    internal class Program
    {

        private static void Main(string[] args)
        {
            var itemServiceFactory = new Mock<IItemServiceFactory>();
            var chrisItem = new Item {Name = "Chris's Amazing Item", Price = 1000000};
            itemServiceFactory.Setup(factory => factory.Create(true).GetItem())
                .Returns(chrisItem);

            var itemManager = new ItemManager(itemServiceFactory.Object);

            var theItem = itemManager.GetAnItem();

            Console.WriteLine("The item is {0} and costs {1}", theItem.Name, theItem.Price);

            var itemServiceFactoryBroken = new Mock<IItemServiceFactory>();
            itemServiceFactoryBroken.Setup(factory => factory.Create(false).GetItem()).Returns(chrisItem); //expecting this to fail, because IItemServiceFactory.Create(true) is configured

            itemManager = new ItemManager(itemServiceFactoryBroken.Object);
            theItem = itemManager.GetAnItem();

            Console.WriteLine("The item is {0} and costs {1}", theItem.Name, theItem.Price); //would expect the item would be null or values to be blank
        }
    }
}
@christopherbauer christopherbauer changed the title Mocks of factories with parameters behave as if you use "It.IsAny(x)" if you mock a function call in the returned object. Bug: Mocks of factories with parameters behave as if you use "It.IsAny(x)" if you mock a function call in the returned object. Jan 3, 2015
@stakx
Copy link
Contributor

stakx commented Jun 16, 2017

Duplicate of #142? (Except this uses new Mock<T>().Setup(...) and the other issue uses Mock.Of<T>(...). Both should work.)

@stakx
Copy link
Contributor

stakx commented Jul 13, 2018

Closing this, this problem will be tracked in #643 (together with a couple other related issues).

@stakx stakx closed this as completed Jul 13, 2018
stakx added a commit to stakx/moq that referenced this issue Mar 8, 2019
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

2 participants