How can I test that a particular method was called with the right parameters as a result of a test? I am using NUnit.
The method doesn't return anything. it just writes on a file. I am using a mock object for System.IO.File
. So I want to test that the function was called or not.
You can verify using MOQ using the Verify method. Like this: var tokenManagerMock = new Mock<ITokenManager>(); var sut = new WhateverItIsCalled(tokenManagerMock. Object); sut.
NUnit Assert class is used to determine whether a particular test method gives expected result or not. In a test method, we write code the check the business object behavior. That business object returns a result. In Assert method we match the actual result with our expected result.
The three key steps to using mock objects for testing are: Use an interface to describe the object. Implement the interface for production code. Implement the interface in a mock object for testing.
More context is needed. So I'll put one here adding Moq to the mix:
pubilc class Calc {
public int DoubleIt(string a) {
return ToInt(a)*2;
}
public virtual int ToInt(string s) {
return int.Parse(s);
}
}
// The test:
var mock = new Mock<Calc>();
string parameterPassed = null;
mock.Setup(c => x.ToInt(It.Is.Any<int>())).Returns(3).Callback(s => parameterPassed = s);
mock.Object.DoubleIt("3");
Assert.AreEqual("3", parameterPassed);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With