![]() Note that Moq uses the term "Loose Mock" for the Dynamic mock concept. For example, if you only want to override the method IsDatabaseActive(), and leave the rest of the class as-is, you'll want to use a partial mock and only provide an alternate implementation for IsDatabaseActive(). So if you're only wanting to replace some of the functionality (or properties), and keep the rest, you'll want to use this. Partial Mock A partial mock will use the underlying object's implementation if you don't provide an alternate implementation. In other words, you'll get back a 0 for number types, false for Booleans and a null for any object types. Dynamic Mock With a dynamic mock, any methods/properties which are called by your tests for which you have not provided an implementation will return the default value for the data type of the return value. If any methods/properties are used which you have not provided implementations for, an exception will be thrown. When using Moq, if you have a reference to the mocked object, you can get back to the wrapper object with this helper method: Mock mockWrapper = Mock.Get(mockedObject) Generating Different Mock Typesįor those of you that read my Using Rhino.Mocks Quick Guide, you may recall there are three types of mocks that can be generated by Rhino.Mocks: Strict Mock A strict mock requires you to provide alternate implementations for each method/property that is used on the mock. IFoo mock = MockRepository.GenerateMock() ![]() For Rhino.Mocks, I get back an IFoo object, but Moq returns a Mock and to get the IFoo object, you access it with the. So in this example, I'm creating an mock of IFoo. Moq, on the other hand, creates a wrapper object that contains a reference to the VerifyAllExpectations(), etc) directly to that mocked object. ![]() With Rhino.Mocks, the MockRepository returns to you an instance of the type you're mocking and you apply the mock operators (. One big difference between Rhino.Mocks and Moq is what gets returned when you generate the mock and how you operate against it. ![]() This post hopes to serve as a guide for people making the transition from Rhino.Mocks to Moq. Moq (pronounced "Mock") has become the de facto replacement. It's effectively dead and very unlikely to support future versions of. I've been a big fan of Rhino.Mocks for many years, but the original Rhino.Mocks maintainer (Oren Eini aka Ayende) stated in 2013 that he is no longer maintaining the project and has handed the project over to a new maintainer (Mike Meisinger), but Mike has not been actively maintaining the project. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2022
Categories |