Unit testing question

C

CSharper

I have a class something like the following

class name
{
public string Method1()
{
}
public bool Method2()
{
...
string x = Method1()
....
}
}


I am writing tests for Method2 and when Method1() is called on my
test, I want to pass some orbitary value to make it run. I don't want
the Method1 to execute. I read about Mock, is it the right way to do
it?

Thanks,
 
C

Ciaran O''Donnell

Have a read about dependency injection, you would need to call method1 from
method2 via a delegate which you pass in. then in the test you can pass in a
mock method and in real life pass in method1.
You would probably benefit from having an overload to method2 which doesnt
take a delegate and passes a delegate to method1 to the overload which takes
one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top