You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The true is ignored and behaves like an It.IsAny<bool>().
using System;using Moq;namespaceMoqBugMCV{publicinterfaceIItemServiceFactory{
IItemService Create(boolshouldCreateServiceA);}publicclassItem{publicstringName{get;set;}publicdecimalPrice{get;set;}}publicinterfaceIItemService{
Item GetItem();}publicclassItemManager{privatereadonlyIItemService_itemService;publicItemManager(IItemServiceFactoryitemServiceFactory){_itemService= itemServiceFactory.Create(true);//<==== configured true (like by app.config at runtime or something)}public Item GetAnItem(){return _itemService.GetItem();}}internalclassProgram{privatestaticvoidMain(string[]args){varitemServiceFactory=newMock<IItemServiceFactory>();varchrisItem=new Item {Name="Chris's Amazing Item",Price=1000000};
itemServiceFactory.Setup(factory => factory.Create(true).GetItem()).Returns(chrisItem);varitemManager=new ItemManager(itemServiceFactory.Object);vartheItem= itemManager.GetAnItem();
Console.WriteLine("The item is {0} and costs {1}", theItem.Name, theItem.Price);varitemServiceFactoryBroken=newMock<IItemServiceFactory>();
itemServiceFactoryBroken.Setup(factory => factory.Create(false).GetItem()).Returns(chrisItem);//expecting this to fail, because IItemServiceFactory.Create(true) is configureditemManager=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}}}
The text was updated successfully, but these errors were encountered:
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
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:
The
true
is ignored and behaves like anIt.IsAny<bool>()
.The text was updated successfully, but these errors were encountered: