When to MockObject?

In my previous post I showed how to mock future instances. Now we will see when to use MockObject.

What is the difference?

The difference is quite simple. But before we delve into it we should talk about the 2 objects that are associated with each mock.

Each mock has a controller that controls the behavior. This is done using the Mock type. Here we setup the Expectations and Validations. And each mock has the actual instance that is being mocked.

Using MockManager.Mock will mock a future instance.
Using MockManager.MockObject will immediately create a mocked instance. It is possible to access this instance using the mock.Object property. (Other mocking frameworks have only this option)

After creating a MockObject we can then pass this object as an argument to our tested method to inject the Mock.

[Test] public void MockObjectTest() { MockObject controlListener = MockManager.MockObject(typeof(MyListener)); MyListener mockedListener = (MyListener)controlListener.Object; // Setup expectation // inject our mocked object into our test Messages.Broadcast("I am alive",mockedListener); // Assertions... }

Note: There is no need for MyListener to be an interface, it could even be a sealed class!

What about interfaces?

When the mocked type (MyListener) is an interface or an abstract class TypeMock will create a new Type on-the-fly, that extends the mocked interface. This new object can then be passed to our tested code.

Mocks returning Mocks

A mocked expectation can return another mock. Here is an example

public class MyFactory { public MyListener GetListener() { // Get Listener through some complex external services } }

We can now setup the expectations that GetListener() will return a mocked MyListener.

[Test] public void MockObjectTest() { // mock future instance of MyFactory Mock factory = MockManager.Mock(typeof(MyFactory)); // create a MockObject of MyListener MockObject controlListener = MockManager.MockObject(typeof(MyListener)); MyListener mockedListener = (MyListener)controlListener.Object; // Setup expectation factory.ExpectAndReturn("GetListener",mockedListener); }

Summary

Here is a list of places where you should use MockObject.

  1. When a mocked object is passed to the tested method.
  2. When you need to mock an abstract class and interface.
  3. When a mocked method returns a mocked type
9 comments
  1. sponge kidney is a developmental anomaly. In most cases, however, ultrasound cannot distinguish medullary sponge kidney from other types of medullary. The association of medullary sponge kidney withMedullary sponge kidney ultrasound diagnosis

  2. Tale modalità prevede la possibilità di giocare nelle mappe base del gioco avendo quali compagni di fazione e quali nemici dei bot, la cui intelligenza artificiale può essere gestita tramite un

  3. Jean Walden to Andrew D. Whitman and Danielle Tardie, 54 Turnpike Road, $176,000. cheap nfl jerseys If we let them, they will put Google and Apple to shame and one day lead the world again in education. s coach outlet
    We need to coordinate with other actors. michael kors outlet 5 million would allow them to switch from ad-hoc operations to more permanent operations, Milstead said.
    Hales and his wife, Nancy, are expected to welcome women to the shelter on Thanksgiving and serve pumpkin pie. michael kors bags My Obi-Wan Kenobi was the store’s manager, Joe, who became my mentor and unofficial big brother; and it was with him that I first saw The Empire Strikes Back. k coach outlet store online
    These are the Saturday and Sunday games at the end of the tournament’s second week cheap jordan shoes The Witch sets its premise quickly, with the father of the Calvinist family, William (Ralph Ineson), effectively getting his family banished from a well-protected camp in the new world sometime in the 1630s.
    p Ann M. Manna, representative, and Patricia A. Arena, estate, to Donald D. Fowler and Rebekah M. Fowler, 66 Nathaniel St., $68,000. michael kors outlet store This year’s Kentucky team isn’t as talented as some, but it is still a good team. michael kors outlet store
    Monday for a report of gunshots. ray ban sunglasses outlet Hewer said the association was concerned about the fee increases but supports the goal of the consumer health and food safety division becoming self-sufficient. http://o.storeonlinecc.com
    When has it happened previously? louis vuitton handbags Auditions q michael kors outlet online
    But this was to be Reynolds’ day. cheap nfl jerseys Austin Dillon finished third, earning a $100,000 bonus.
    Until You ve Read ThisThank you for your feedbackWe ll review and make changes needed. http://www.raybanoutlet.name Burns filed a civil antitrust lawsuit in federal court in Oklahoma on Thursday against Chesapeake, SandRidge Energy Corp.
    It s increasingly difficult to argue that applicants are being blindsided by schools digging into their social media history online. michael kors bags The master-planned community, along Tangerine Road and west of Interstate 10, is 1,325 acres in size and has more than 1,250 homes.
    Gallardo, Trumbo and Alvarez aren’t stars, but they should be modest upgrades. coach outlet “He hasn’t won anything.
    p Heroin. louis vuitton handbags As the scoreboard shows, the offense was able to score points but the Tiger defense was just as tough in the trenches.
    There’s a ripple effect from this class that will be felt for many years. louis vuitton outlet My education began on one of the docent-led hikes offered each spring by the Santa Clara Valley Open Space Authority. c http://www.michaelkorsoutletbuy.us.com
    “The operation was more sophisticated than just a color printer and copier,” said Sgt. http://www.raybansunglasses.name The more and more I use it, I think this could be a handy thing for a lot of people.
    a Barry Carman, representative, Arlene H. Howes, estate, Arlene H. Kavanagh, estate, and Dean L. Kavanagh to Scott Humble, 38 Chiswick St., $182,500. michael kors online MondayThursday, 10 a. m
    In at least one case the office was an old Allstate Canada address, even though Onward Assurance Financial Services is not affiliated or associated with Allstate Insurance Company of Canada which is licenced with FSCO ray ban sunglasses The reporting was funded in part by a grant from the Corporation for Public Broadcasting. http://www.raybanoutlet.name
    Annie Chen Zheng, Xin Chun Zheng and Xin Chen Zheng to Peter Goodale and Sandy Goodale, 180 Stony Hill Road, $235,000. louis vuitton outlet online Brian Bowen, Little Rock, to the Arkansas State Independent Living Council. s coach outlet
    Affinity Sports is the largest and most successful integrated software service provider to state youth and adult soccer associations in the USA. http://www.raybansunglassesbuy.co.uk Suu Kyi’s two sons are British, as was her late husband.
    The Happier Valley Comedy Show bring on of its most popular events to the on Oct. 10. “Scripted/Unscripted” features the unpredictable happenings that occur when you combine one performer sticky following a script line by line with another one, who has no idea what the script is and simply has to improvise. The night will feature a group of such scenes, pairing respected local actors with the area’s top improvisers. The doors open at 7 p.m. The Arts Block Cafe is at 289 Main St. in Greenfield. http://www.raybanoutlet.name EU President Donald Tusk said that after a week of shuttle diplomacy in and around Turkey, “we have a breakthrough now. m ray ban outlet
    SynopsisVerdict Financial s “UK Travel Insurance: Distribution and Marketing” explores consumer purchasing behavior and how it is changing. http://www.louisvuittonhandbagsrack.us.com Tarczewski was whistled for a technical foul while he argued a call from under the UA basket, allowing ASU to creep within 48-43 on two free throws, while UA coach Sean Miller took his jacket off in apparent displeasure that a foul was not called when Kadeem Allen drove inside for a basket.

  4. I was wondering if you ever thought of changing the page layout of your site? Its very well written; I love what youve got to say. But maybe you could a little more in the way of content so people could connect with it better. Youve got an awful lot of text for only having one or 2 pictures. Maybe you could space it out better?

Add Comment

Required fields are marked *. Your email address will not be published.