I would like to clear things up as this is a delicate distinction.
Dror's explanation is right but actually it's the other way around:
- Mock is used to set behavior and expectations of
objects that have not yet been created. A Mock will become active once an object of the type being mocked is instantiated.
- MockObject on the other hand
creates an object to work with and is used to set behavior and expectations of that specific instance.
An after effect of this distinction is that interfaces and abstract classes cannot be used in conjunction with Mock - instances cannot be created for these constructs so a Mock of that type would never become active. The aforementioned error message tells you right that - cannot use Mock with an interface or abstract class, use MockObject instead. MockObject instantiates an instance of the class (Typemock Isolator can do that even for interfaces and abstract classes) which eliminates the problem.
Confusing? we think so too... This confusion is cleared up with the AAA syntax, and you don't need to mess around with Mocks/MockObjects. Instead, you work with a fake instance of the class being faked, and you can define it will be used on a future instantiation. Also, you can use the Recursive Fakes feature that allows you to fake an entire object model instead of messing with setting each call's return value:
// create a recursive fake of ILoader - calls to ILoader methods will return fake values
ILoader fakeLoader = Isolate.Fake.Instance<ILoader>(Members.ReturnRecursiveFakes);
// Swap the next instantiation of LoaderClass with the fake loader
Isolate.Swap.NextInstance<LoaderClass>().With(fakeLoader);
// Thats all the set up you need (2 lines instead of 4...
// Now we run the test and assert the results
Service service = new Service();
Assert.IsNotNull(service.GetProduct());
Please let us know if this works for you, or if you need any help using AAA.
Doron
Typemock support