c# - Discrepancies between “Mock” Database and “Real” database behaviours -
We use C # and Linq2SQL with a MS SQL Server database. To conduct some unit testing, we have a moccadacttext, based on the use of "real" or "fake" database, we search for two different behaviors when testing.
Scenario 1: Real Database
The database has 5 records:
db = realDatabase db .InsertOnSubmit (new record) var count1 = db.getTable.Count () db.SubmitChanges () var count2 = db .getTable.Count () count1 = 5 count2 = 6
Scenario 2: Duplicate database
Database has 5 records:
db = mockDatabase db.InsertOnSubmit (new record) var count1 = db.getTable.Count () Db.SubmitChanges () var count2 = db.getTable.Count () Count1 = 6 count2 = 6
* The "fake" database is called the first submit changelog () Therefore it is included in the counting of the new records before know about. For testing, we need the similarity of the two behaviors.
Has anyone else come in this problem and can you recommend any solution? IMO, this is a common mistake that tries to emulate in tests
If fake behavior is complex, then you end up in your fake test rather than your business code.
I am using Rhinomox, and look like this:
// IList < Record & gt; Testdata = new list & lt; Record & gt; () {A, b, c}; DB = Mock Repository. Generatormock & lt; IDbs & gt; (); Db.Stub (x => db.getTable) .Return (testdata); // ACT: Call your unit under test / db.AssertWasCalled (x => x.InsertOnSubmit (Arg .is.Anything)); Db.AssertWasCalled (x = & gt; x.SubmitChanges ()); It returns back to the same list every time for many cases, it will be enough you can still return other data to the second getTable call:
< Code> db.Stub (x = & gt; db.getTable) .Return (testdata1); Db.Stub (x = & gt; db.getTable) .Return (testdata2);
It is always specific for the same test, but it makes it so simple.
Edit:
I have to admit that I am not familiar with Linq2Sql, in my example, there are fun calls in the Linq2Sql call, which probably can not be easily removed. Perhaps it should be kept behind a simple DAL interface. Then you duplicate this interface.
Comments
Post a Comment