Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit Testing Repositories
    text
    copied!<p>In my project I am following Repository Pattern. My Repositories talk to the Persistence Layer and I am using Entity Framework as the ORM.</p> <p>Currently, in my unit tests, I code directly against the Persistence Layer and as a result any test runs directly against the DB. So, if I test for insertion, it actually inserts the data into the Database.</p> <p>I want to know how can I test without having to worry about database getting modified?</p> <p>Is there a way I can do in-memory database testing?</p> <p>Thanks</p> <p>I have created a Database schema similar to actual one in my App_Data and then I tried implementing it like this -</p> <pre><code> [TestInitialize] public void TestInitialize() { //TODO: in TestCleanup, do a rollback to revert any changes performed during the test. Database.SetInitializer&lt;LogViewerDbContextFake&gt;(new LogViewerInitializer()); } [TestMethod] public void Get_Should_Return_List_Of_Applications() { // Arrange LogViewerDbContext testDbContext = new LogViewerDbContextFake(); ApplicationRepository sut = new ApplicationRepositoryImpl(testDbContext); // Act List&lt;string&gt; failure = sut.Get(); // Assert Assert.AreEqual(4, failure.Count); } internal class LogViewerInitializer : DropCreateDatabaseIfModelChanges&lt;LogViewerDbContextFake&gt; { protected override void Seed(LogViewerDbContextFake context) { List&lt;LogEntry&gt; logEntries = new List&lt;LogEntry&gt;() { new LogEntry{ Id = 1, Application = "Application 1" }, new LogEntry{ Id = 2, Application = "Application 2" }, new LogEntry{ Id = 3, Application = "Application 3" }, new LogEntry{ Id = 4, Application = "Application 4" }, new LogEntry{ Id = 5, Application = "Application 2" }, new LogEntry{ Id = 6, Application = "Application 2" } }; logEntries.ForEach(s =&gt; context.LogEntries.Add(s)); context.SaveChanges(); } } </code></pre> <p>Even though test ran and passed, LogViewerInitializer did not insert any records in the App_Data\Database. So, I was wondering what is the logic behind and how exactly things are getting executed?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload