@@ -298,6 +298,41 @@ public CustomerRepositoryTest(SqlServerDatabaseInitializer initializer)
298
298
Now, every time we will execute an test in the ` CustomerRepositoryTest ` class,
299
299
a database will be deployed with these 3 ` Customer ` before the execution of the test.
300
300
301
+ ### Initializes the data from a T-SQL script
302
+
303
+ Since the version 2.1.0, it is possible to execute a T-SQL script by using the ` SqlServerDatabase.ExecuteScript() ` method.
304
+
305
+ For example, if we want to initialize the previous customers using a T-SQL, add a ` .sql ` file in the project with the following code:
306
+
307
+ ``` sql
308
+ INSERT INTO [Customer] ([FirstName], [LastName], [Revenue])
309
+ VALUES
310
+ (' John' , ' DOE' , 110 .50 ),
311
+ (' Marcel' , ' DUPONT' , 4852 .45 ),
312
+ (' Andres' , ' GARCIA' , 0 )
313
+ ```
314
+
315
+ In Visual Studio, set the following project properties for the ` InsertData.sql ` file:
316
+ - ` Build Action ` : ` Content ` . This option will recompile the test project every time the T-SQL script has been changed.
317
+ - ` Copy to Output Directory ` : ` Copy if newer ` . This option allows the compiler to copy the file in the output directory and
318
+ make it available for the tests in the current directory when execution the tests.
319
+
320
+ In the constructor of the ` CustomerRepositoryTest ` call the script using the ` SqlServerDatabase.ExecuteScript() ` method.
321
+
322
+ ``` csharp
323
+ public CustomerRepositoryTest (SqlServerDatabaseInitializer initializer )
324
+ {
325
+ using var dbContext = new DemoAppDbContext (DatabaseTestsConnectionStrings .CreateDbContextOptions <DemoAppDbContext >(DatabaseName ));
326
+
327
+ this .database = initializer .Initialize (dbContext );
328
+
329
+ this .database .ExecuteScript (File .ReadAllText (" InsertData.sql" ));
330
+ }
331
+ ```
332
+
333
+ Now, every time we will execute an test in the ` CustomerRepositoryTest ` class,
334
+ a database will be deployed and the ` InsertData.sql ` will be executed.
335
+
301
336
## Write the tests for methods that retrieve data
302
337
303
338
This section describes how to write an test for methods that retrieve data (SELECT queries).
0 commit comments