Description
What are the best practices to do unit testing with SQLProvider?
Let's say that I have a method:
let myLogic email =
let usr =
query {
for u in dbContext.MyProject.Useraccount do
where (u.Email = email && u.Disabled.IsNone)
select (u)
} |> Seq.head
usr.Accessed <- DateTime.UtcNow
dbContext.SubmitUpdates()
usr
How would you unit-test this?
Should we have a way to mock data to produce just some noise out of LINQ-expressions and dbContext?
Like a separate nuget-package as SQLProvider.Unittest.Datagenerator or something?
E.g. FsCheck is producing some random noise over types, could you use FsCheck to mock dbContext?
Or as conversation, how would you like to test that if you could decide?
One example: You can have dbContext creation in separate fsproj and in unit-test time you use instead a mock like:
open System
open System.Linq
type MockUser = {
Email: string;
Disabled: bool Option;
mutable Accessed: DateTime
}
type MockDatabase = {
Useraccount: System.Linq.IQueryable<MockUser>
}
type MockContext = {
MyProject: MockDatabase
} with
member __.SubmitUpdates() =
Console.WriteLine("Saved")
let dbContext =
{ MyProject =
{ Useraccount =
[{
Email = "[email protected]";
Disabled = Some false;
Accessed = DateTime.MinValue
}].AsQueryable()
}
}
// ...and now the previous LINQ is perfectly valid without database.
[<Fact>]
let ``my unit test`` =
let res = myLogic "[email protected]"
Assert.Equal("[email protected]", res.Email)
Assert.True(res.Accessed > DateTime.MinValue)
...but we could also generate these mocks automatically via calling FSharp-compiler on runtime to analyse the source code, like @pezipink's metaprovider, right? A unit-test-typeprovider?