Re: Unit Testing Model-View-Presenter

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Therein lies the problem, I can't seem to figure out how to properly
attach to the presenters from my unittest code, and then call InitView
without my production presenters actually connecting to the DB and
retirieving data.

Have you got a high level example?

Could be I am not exactly understanding the approach...

SN

Let's say I have a Presenter that presents a username from the
database.

within the InitView method, I would be attaching the view to the model
like so:


public void InitView() {
ISession session = NHibernate.Session() // just for the example
this.view = session.Get(typeof(User), id); // User is the class
that attaches to the view
}

Now, this code is fine in the production enviorment, but in my unit
test, I don't want to be retrieving any data from the backend, I want
to mock the User object, but then how do I actually get the mock in
there, thats why I made the test method like so:

public void InitViewTest() {
User user = new User();
user.UserName = "sean";
this.view = user;
}

I guess what I'm looking for here, is what is the proper way to supply
my presenter with a mock object, so that I can effectively test MVP.
From all the books I've read, they always stress not to delve to far
into the chain while unit testing.

I think I'm just missing something simple here, I am imagining that the
piece I'm missing is Dependency Injection, but I'm not sure

Thanks

Sean

.