Apps72 / DbMocker

Data Mocker for C# DbConnection
MIT License
31 stars 11 forks source link

Mocking Multiple queries #30

Open psyciknz opened 2 years ago

psyciknz commented 2 years ago

I have managed to successfully mock a query using:

var conn = new Apps72.Dev.Data.DbMocker.MockDbConnection();
            conn.Mocks
                .When(cmd => cmd.CommandText.StartsWith("SELECT 1 FROM "))
                .ReturnsScalar<int>(1);

Then in one method I have the following query that matches teh command text:

command.CommandText = "SELECT 1 FROM " + this.headerTable + " WHERE JobID='" + this.jobId  '";
int result = command.ExecutNonQuery();

But then further down in teh same method I have

command.CommandText = "SELECT 2 FROM " + this.headerTable + " WHERE JobID='" + this.jobId  '";
int result = command.ExecutNonQuery();

How can I add a mock result for startswith("Select 2 from")

Regards

dvoituron commented 2 years ago

Hello, That's a good question that I will add in the documentation. It's easy: you can add multiple Mocks.When for the same MockDbConnection.

conn.Mocks
    .When(cmd => cmd.CommandText.StartsWith("SELECT 1 FROM "))
    .ReturnsScalar<int>(1);

conn.Mocks
    .When(cmd => cmd.CommandText.StartsWith("SELECT 2 FROM "))
    .ReturnsScalar<int>(2);

Don't forget that you have also the method WhenTag to help you to detect the correct SQL Query. See https://apps72.com/dbmocker/conditions.html#whentag

psyciknz commented 2 years ago

Hey thanks for the answer overngiht.

I did see the tagging, but correct me if I'm wrong, tagging a command/query etc is not part of the out of the box system.data.x namespaces is it?