Some basic Moq usage and Moq usage
This article describes how to use Moq. First install Moq through NuGet. Including:
- Return Value of the simulated method
- Run the callback function after simulating the method.
- The simulation method returns multiple values in sequence.
- An exception is returned when the second call method is simulated.
- Directly return the original return value of the simulated method
- Simulate generic classes
Return Value of the simulated method
public class HelperClass
{
public virtual Boolean IsEnabled()
{
throw new Exception();
}
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var mock = new Mock<HelperClass>();
mock.Setup(x => x.IsEnabled()).Returns(true);
Assert.AreEqual(mock.Object.IsEnabled(),true);
}
}
● Moq simulation methods are generally virtual Methods and interface methods.
● Mock. Object refers to the HelperClass instance.
Run the callback function after simulating the method.
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
string temp = "";
var mock = new Mock<HelperClass>();
mock.Setup(x => x.IsEnabled()).Returns(true).Callback(() => { temp = "success"; });
Assert.AreEqual(mock.Object.IsEnabled(),true);
if (temp == "success")
{
//TODO
}
}
}
● After the Return method is executed, the Callback method is executed. The received parameter type is delegate.
The simulation method returns multiple values in sequence.
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var mock = new Mock<HelperClass>();
mock.SetupSequence(x => x.IsEnabled())
.Returns(true)
.Returns(false);
Assert.AreEqual(mock.Object.IsEnabled(),true);
Assert.AreEqual(mock.Object.IsEnabled(), false);
}
}
● The SetupSequence method can return multiple values for the method.
If you put the following two sentences:
Assert. AreEqual (mock. Object. IsEnabled (), true );
Assert. AreEqual (mock. Object. IsEnabled (), false );
Turn the order upside down:
Assert. AreEqual (mock. Object. IsEnabled (), false );
Assert. AreEqual (mock. Object. IsEnabled (), true );
Result: The test fails. Because the returned values of the Methods returned by the susuupsequence are ordered.
An exception is returned when the second call method is simulated.
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var mock = new Mock<HelperClass>();
mock.SetupSequence(x => x.IsEnabled())
.Returns(true)
.Throws(new Exception());
Assert.AreEqual(mock.Object.IsEnabled(),true);
Assert.AreEqual(mock.Object.IsEnabled(), true);
}
}
● Throws method Throws an exception when the IsEnable () method is called for the second time.
Directly return the original return value of the simulated method
public class HelperClass
{
public virtual Boolean IsEnabled()
{
throw new Exception();
}
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var mock = new Mock<HelperClass>();
mock.CallBase = true;
mock.SetupSequence(x => x.IsEnabled())
.CallBase();
Assert.AreEqual(mock.Object.IsEnabled(), true);
}
}
The IsEnabled method throws an exception in the simulated HelperClass class. When the CallBase attribute of the Moq instance is set to true and the CallBase method is called, the original return value of the method in the simulated class can be directly returned in the test method.
Simulate generic classes
public class HelperClass<T> where T : class
{
public virtual Boolean IsEnabled()
{
throw new Exception();
}
}
public class Sample{}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var mock = new Mock<HelperClass<Sample>>();
mock.SetupSequence(x => x.IsEnabled()).Returns(true);
Assert.AreEqual(mock.Object.IsEnabled(), true);
}
}
Basic usage of word
If you are new to this office software, I suggest you buy a book.
What is the second usage when the current task is completed? Write two examples
Basic Structure: have/has + done
Negative Form: have/has + not + done.
Common question: have or has.
The current completion without time inactive indicates that the action has been completed before the speech, and the consequences and effects have existed so far.
I have seen the film hours times.
The city has taken on a new look.
Indicates the duration of the status, status, and action that persists until now.
The conference has lasted five days.
I have learned English for 5 years.