Unit testing question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CSharper

    Unit testing question

    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,

  • =?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=

    #2
    RE: Unit testing question

    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.

    --
    Ciaran O''Donnell
    try{ Life(); } catch (TooDifficultException) { throw Toys(); }



    "CSharper" wrote:
    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,
    >
    >

    Comment

    Working...