views:

48

answers:

1

Hi all,

I have a method that I want to test which hits the database. From what I have read this is a perfect oppurtunity to use a mock. However the problem that I am facing is that I pass the object a string and then it creates an object and hits the db with this object i.e.

public void test(string t)
{
   Test t1 = new Test(t);
   db.Save(t1);
}

Then the in the test I have:

using(mockery.Record)
{
   Expect.Call(db.Save( ??? ))
}

The problem being - what do I expect here? A call to:

Expect.call(db.Save(new Test(t))

does not work.

As I am new to mocking this may be an easy question, but any help will be much appreciated.

thanks

A: 

Hi.

Well often you pass the actual instance which you later expect. For example:

public void test(string t)
{
    Test t1 = new Test(t);
    using(mockery.Record)
    {
       Expect.Call(db.Save(t1));
    }
    using(mockery.Playback()
    {       
        db.Save(t1);
    }
    mockery.VerifyAll();
}

But this is maybe to limiting. Then you can actually define criterias which the argument has to fullfill. For example:

using(mockery.Record)
{
   Test instanceToCompare = new Test(t);    
   Expect.Call(db.Save(Arg<Test>.Is.Equal(instanceToCompare)));
}

Or another example:

using(mockery.Record)
{
   Expect.Call(db.Save(Arg<Test>.Matches(t=>t.Name.Length.Equals("Test"))));        
}
Gamlor