chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I have a mocked interface as following:
interface ILogger {
     void Log(string message);
}


I want to verify the following:
1) Log() was called once with argument "start"
2) Log() was NOT called with argument "stop"

How can I do this?
asked by doug (3.1k points)

3 Answers

0 votes
Hi,

Please find an example below:

interface ILogger
{
    void Log(string message);
}

[TestMethod]
public void LoggingTest()
{
    //ARRANGE
    ILogger logger = Isolate.Fake.Instance<ILogger>();                    

    //ACT
    logger.Log("start");

    //ASSERT
    Isolate.Verify.WasCalledWithExactArguments(() => logger.Log("start"));
    Isolate.Verify.WasCalledWithArguments(() => logger.Log(null)).Matching(args =>
        {
            string inParam = (string)args[0];
            return !inParam.Equals("stop");
        });
}


Let me know if that is what you're looking for.
answered by alex (18k points)
0 votes
Thanks for the reply. I wasn't clear enough in my original question. My Log() is called multiple times during a test and that is where my issues are coming from I think.

Please let me know if your answer still applies if this was the act portion of the test.

//ACT
logger.Log("start");
//other stuff
logger.Log("error");


Additionally, is this a correct understanding of how the WasCalledWithArguments.Matching() chain works:
1) Due to multiple Log() calls, Matching's predicate will be called TWICE, returning TRUE one time and FALSE the other?
2) WasCalledWithArguments will OR the results, and fail if that OR's results in false?

Thanks.
answered by doug (3.1k points)
0 votes
Hi,
The answer is agnostic to the number of calls to logger.Log("") so it will apply to your example as well.
1) Due to multiple Log() calls, Matching's predicate will be called TWICE, returning TRUE one time and FALSE the other?
Matching's predicate will indeed be called for each value passed to logger.Log(VALUE) and fail when the predicates condition is not fulfilled. Since the predicate in my example returns false only if "stop" was passed to logger.Log() int the following case
//ACT
logger.Log("start");
//other stuff
logger.Log("error");
the predicate will return TRUE both times.

2) WasCalledWithArguments will OR the results, and fail if that OR's results in false?
You got it right :D
answered by alex (18k points)
...