Conditions
When
Use the When
method to describe the condition to be detected.
This condition is based on a Lambda expression containing
a CommandText or Parameters check.
conn.Mocks
.When(cmd => cmd.CommandText.Contains("SELECT COUNT") &&
cmd.Parameters.Count() == 0)
.ReturnsTable(...);
Based on this condition, when a SQL query will be detected,
the Returns
values will be sent.
WhenTag
Use the WhenTag
method to detect query containing a row starting
with a comment -- MyTag
. This is compatible with EFCore 2.2,
containing a new extension method TagWith
to identity a request.
conn.Mocks
.WhenTag("MyTag")
.ReturnsTable(...);
Best practice: use a Tag to easily find your SQL queries. Each request in your application must have the equivalent Tag (via a SQL comment or by using the
TagWith
method)
See the TagWith method included in Database Command toolkit.
WhenAny
Use the WhenAny
method to detect all SQL queries.
In this case, all queries to the database will return the data specified
by WhenAny.
conn.Mocks
.WhenAny()
.ReturnsTable(...);
LoadTagsFromResources
To avoid creating dozens of .WhenTag().ReturnsTable()
, you can use the method LoadTagsFromResources
.
This method search all text files embedded in your projects and use the name as the Tag Name.
Checking order
DbMocker uses the condition encoding order to return the correct table.
We recommend creating a new instance of MockDbConnection for each unit test. Indeed, the list of Mocks Conditions is global at the SQL connection.