Wednesday, February 22, 2017

cannot convert from 'Moq.Mock<IOrderDataService>' to 'IOrderDataService'

As a way of trying to solidify (no pun intended) test driven development in my head, I have been following along the Telerik blog
"Thirty Days of TDD Day 12: working with stubs"
www.telerik.com/blogs/30-days-of-tdd-day-12-working-with-stubs
I already have a couple of Mocking frameworks installed and I'm sure JustMock is great but my registry is messed up enough already so
I thought I'd translate to Moq and see how I went.
Wehn I try to run the tests I can the very obscure error


Error    2    Argument 1: cannot convert from 'Moq.Mock'<TddStore.Core.IOrderDataService>' to 'TddStore.Core.IOrderDataService'

apparently this is a fairly common Moq beginner error and posting the error here in case it will save somebody a few minutes


              var orderDataService = new Mock();
              orderDataService.Setup(x => x.Save(It.IsAny())).Returns(expectedOrderId);

              var orderService = new OrderService(orderDataService);


and instead it should be
var orderService = new OrderService(orderDataService.Object);

As always Stack Overlow saved the day, once I figured out how to word a search

http://stackoverflow.com/questions/7011572/passing-moq-mock-objects-to-constructor