Posts tagged with “test”

Moq Tips 2 - check if a log message with a specific prefix/suffix and call a method with an object having a specific field value

  1. To check if a log message with a specific prefix is logged, you can use Moq's Verify method along with an appropriate assertion.
_logger.Verify(logger => logger.logInformation(
    It.Is<string>(s => s.StartsWith("specific prefix"))), Times.Once);
  1. To verify that a method is called with an object containing a specific property value, you can use Moq's It.Is<T> matcher along with a lambda expression. Here's an example:

    _mockObject.Verify(x => x.MyMethod(It.Is<MyObject>(
     obj => obj.PropertyValue == expectedPropertyValue)), Times.Once);
  2. Correctly returns null: Sometimes we need to set up a Moq mock to return null but cannot directly do it as Moq doesn't allow. Please check the following way that you should use:

    b => b.GetBookStoreAccountByUserName(It.IsAny<string>()))

The key is to use (BookStoreAccount)null to explicitly cast null to the expected return type.

Approval Testing is tasty

Just record a few tips here:

[UseApprovalSubdirectory("Approvals")] can be used to set up the directory that saves the approval results.

It can be used for a test class or a test method.


ApprovalTests.Net Project

Using the cookie feature in Insomnia Core

It took me more than an hour to understand how to use cookies when you are sending a request to the API server.

The most important thing is that you only need to correctly add the cookies, then the cookies will be automatically sent to the API server. Don't try to add a cookie item at the headers' configuration part!