Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Professional Test Driven Development with C#: Developing Real World Applications with TDD
This is the forum to discuss the Wrox book Professional Test Driven Development with C#: Developing Real World Applications with TDD by James Bender, Jeff McWherter; ISBN: 978-0-470-64320-4
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Test Driven Development with C#: Developing Real World Applications with TDD section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 18th, 2015, 02:17 PM
Registered User
Points: 5, Level: 1
Points: 5, Level: 1 Points: 5, Level: 1 Points: 5, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2015
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to mock a dependency injection in a dependent class

Hi,

I'm trying to get to grips with TDD and am at the stage where I am trying to mock database access. I have closely followed Chapter 5 and have set up Ninject etc. However, in my real life application, my equivalent to the BusinessService class takes an IDialogService as a parameter. The reason for this is to stop Message Boxes displaying when running the unit tests. I therefore have the following line in the module:

Bind<IDialogService>().To<DialogService>();

The implementation of IDialogService is as follows:

public interface IDialogService
{
void ShowMessageBox(string caption);
}

public class DialogService : IDialogService
{
public void ShowMessageBox(string caption)
{
MessageBox.Show(caption);
}
}

public class DialogSupressedService : IDialogService
{
public void ShowMessageBox(string caption)
{
return;
}
}

I use DialogService in my application and DialogSupressedService in my unit test. However, when I create an instance of my equivalent to the BusinessService class using the Ninject Kernel, I can't figure out how to force it to use the DialogSupressedService in the unit test.

I hope I have explained this clearly.

Thanks in advance for any help.

Edit: I have realised one solution to this is to maintain two separate module files i.e. one for unitest containing:

Bind<IDialogService>().To<DialogSupressService>();

and one for the actual application containing:

Bind<IDialogService>().To<DialogService>();

Is this the right way to go?

Last edited by smithy; August 18th, 2015 at 03:48 PM.. Reason: Possible solution


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 12: Dependency Injection rupen BOOK: Professional ASP.NET MVC 4 1 April 21st, 2014 11:28 PM
Chapter 7: Unit Test Only Tests the Mock? SleepyCatChris BOOK: Professional Test Driven Development with C#: Developing Real World Applications with TDD 1 March 27th, 2013 10:54 AM
Dependency Inversion Principle Richard Smith BOOK: Professional Enterprise .NET 0 August 17th, 2010 08:21 PM
dependency files m002864 Crystal Reports 0 July 7th, 2004 02:56 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.