@@ -10,12 +10,35 @@ namespace DbMocker.Tests
1010 [ TestClass ]
1111 public class Samples
1212 {
13+ [ TestMethod ]
14+ public void UnitTest0 ( )
15+ {
16+ var conn = new MockDbConnection ( ) ;
17+
18+ // The text file "123-EMPLOYEES.txt" is embedded in this project.
19+ // See the Samples folder.
20+ // - 123 is an identifier (as you want)
21+ // - EMPLOYEES is the CommandText Tag
22+ // See https://docs.microsoft.com/en-us/ef/core/querying/tags
23+ conn . Mocks . LoadTagsFromResources ( "123-EMPLOYEES" ) ;
24+
25+ // Call your "classic" methods to tests
26+ var data = GetEmployees ( conn ) ;
27+
28+ // DbMocker read the embedded file
29+ // and associated the content to the tag
30+ Assert . AreEqual ( 2 , data . Length ) ;
31+ Assert . AreEqual ( "Scott" , data [ 0 ] [ 1 ] ) ;
32+ Assert . AreEqual ( "Bill" , data [ 1 ] [ 1 ] ) ;
33+ }
34+
1335 // Sample method from your DataService
1436 public int GetNumberOfEmployees ( DbConnection connection )
1537 {
1638 using ( var cmd = connection . CreateCommand ( ) )
1739 {
18- cmd . CommandText = "SELECT COUNT(*) FROM Employees" ;
40+ cmd . CommandText = @"-- COUNT_EMPLOYEES
41+ SELECT COUNT(*) FROM Employees" ;
1942 return Convert . ToInt32 ( cmd . ExecuteScalar ( ) ) ;
2043 }
2144 }
@@ -25,7 +48,8 @@ public object[][] GetEmployees(DbConnection connection)
2548 {
2649 using ( var cmd = connection . CreateCommand ( ) )
2750 {
28- cmd . CommandText = "SELECT ID, Name FROM Employees" ;
51+ cmd . CommandText = @"-- EMPLOYEES
52+ SELECT ID, Name FROM Employees" ;
2953 var reader = cmd . ExecuteReader ( ) ;
3054
3155 var data = new List < object [ ] > ( ) ;
@@ -53,7 +77,7 @@ public void UnitTest1()
5377 // Don't execute the query to your SQL Server,
5478 // But returns this MockTable.
5579 conn . Mocks
56- . When ( cmd => cmd . CommandText . StartsWith ( "SELECT COUNT(*) ") &&
80+ . When ( cmd => cmd . CommandText . Contains ( "COUNT_EMPLOYEES ") &&
5781 cmd . Parameters . Count ( ) == 0 )
5882 . ReturnsTable ( MockTable . WithColumns ( "Count" )
5983 . AddRow ( 14 ) ) ;
@@ -70,7 +94,7 @@ public void UnitTest2()
7094 var conn = new MockDbConnection ( ) ;
7195
7296 conn . Mocks
73- . WhenAny ( )
97+ . WhenTag ( "EMPLOYEES" )
7498 . ReturnsTable ( new MockTable ( ) . AddColumns ( "ID" , "Name" )
7599 . AddRow ( 1 , "Scott" )
76100 . AddRow ( 2 , "Bill" ) ) ;
@@ -86,7 +110,7 @@ public void UnitTest3()
86110 var conn = new MockDbConnection ( ) ;
87111
88112 conn . Mocks
89- . WhenAny ( )
113+ . WhenTag ( "EMPLOYEES" )
90114 . ReturnsTable ( MockTable . Empty ( )
91115 . AddColumns ( "ID" , "Name" )
92116 . AddRow ( 1 , "Scott" ) ) ;
@@ -102,7 +126,7 @@ public void UnitTest4()
102126 var conn = new MockDbConnection ( ) ;
103127
104128 conn . Mocks
105- . WhenAny ( )
129+ . WhenTag ( "COUNT_EMPLOYEES" )
106130 . ReturnsTable ( MockTable . SingleCell ( "Count" , 14 ) ) ;
107131
108132 int count = GetNumberOfEmployees ( conn ) ;
@@ -116,7 +140,7 @@ public void UnitTest5()
116140 var conn = new MockDbConnection ( ) ;
117141
118142 conn . Mocks
119- . WhenAny ( )
143+ . WhenTag ( "COUNT_EMPLOYEES" )
120144 . ReturnsTable ( cmd => MockTable . SingleCell ( "Count" , cmd . Parameters . Count ( ) ) ) ;
121145
122146 int count = GetNumberOfEmployees ( conn ) ;
@@ -130,7 +154,7 @@ public void UnitTest6()
130154 var conn = new MockDbConnection ( ) ;
131155
132156 conn . Mocks
133- . WhenAny ( )
157+ . WhenTag ( "COUNT_EMPLOYEES" )
134158 . ReturnsScalar < int > ( 14 ) ;
135159
136160 int count = GetNumberOfEmployees ( conn ) ;
@@ -156,5 +180,29 @@ public void UnitTest7()
156180 Assert . AreEqual ( "Scott" , data [ 0 ] [ 1 ] ) ;
157181 }
158182
183+ [ TestMethod ]
184+ public void UnitTest8 ( )
185+ {
186+ var conn = new MockDbConnection ( ) ;
187+
188+ // Sample data rows
189+ var samples = new [ ]
190+ {
191+ new { ID = 0 , Name = "Scott" } ,
192+ new { ID = 1 , Name = "Denis" } ,
193+ } ;
194+
195+ conn . Mocks
196+ . WhenTag ( "EMPLOYEES" )
197+ . ReturnsTable ( MockTable . FromType ( samples ) ) ;
198+
199+ var data = GetEmployees ( conn ) ;
200+
201+ Assert . AreEqual ( 2 , data . Length ) ;
202+ Assert . AreEqual ( 0 , data [ 0 ] [ 0 ] ) ;
203+ Assert . AreEqual ( "Scott" , data [ 0 ] [ 1 ] ) ;
204+ }
205+
206+
159207 }
160208}
0 commit comments