Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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



All times are GMT -4. The time now is 12:55 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.