Rockstart
Rockstart

Reputation: 2377

Assert if Func is called with required parameters

 public class ClassUnderTest
    {
        void Process(Func<string, bool> doSomething)
        {
            //other code
            doSomething("123");
        }
    }

How do I assert that doSomething is called with parameter 123?

Upvotes: 0

Views: 65

Answers (2)

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73492

It's as simple as that. Just create a delegate which does the assertion and pass it to the Process method.

For example:

[Test]
public void SomeTestCase()
{
    ClassUnderTest sut = new ClassUnderTest();
    Func<string, bool> func = (param)=> 
    {
        Assert.That(param, Is.EqualTo("123"));
        return true;//or whatever
    };

    sut.Process(func);
}

Upvotes: 1

Marcin Iwanowski
Marcin Iwanowski

Reputation: 791

You can use for example Rhino Mocks:

someObject.AssertWasCalled(x => x.doSomething("123"));

Check this: https://hibernatingrhinos.com/Oss/rhino-mocks/learn/Usage/assert-that-a-method-is-called-with-a-value-in-expected-state

Upvotes: 0

Related Questions