public void AddBug_WhenBugIsValid_ShouldAddToDb() { // Arrange -> prepare the objects var bug = new Bug() { Text = "Sample New Bug", DateCreated = DateTime.Now, Status = BugStatus.New }; var dbContext = new BugTrackerDbContext(); // Act -> perform some logic dbContext.Bugs.Add(bug); dbContext.SaveChanges(); // Assert -> validate the results var bugInDb = dbContext.Bugs.Find(bug.Id); Assert.IsNotNull(bugInDb); Assert.AreEqual(bug.Text, bugInDb.Text); Assert.AreEqual(bug.Status, bugInDb.Status); Assert.AreEqual(bug.DateCreated, bugInDb.DateCreated); Assert.IsTrue(bugInDb.Id != 0); }
public static void CleanDatabase() { using (var dbContext = new BugTrackerDbContext()) { dbContext.Comments.Delete(); dbContext.Bugs.Delete(); dbContext.Users.Delete(); dbContext.SaveChanges(); } }
public void AddBug_WhenBugIsInvalid_ShouldThrowException() { // Arrange -> prapare the objects var dbContext = new BugTrackerDbContext(); var invalidBug = new Bug() { Text = null }; // Act -> perform some logic dbContext.Bugs.Add(invalidBug); dbContext.SaveChanges(); // Assert -> expect an exception }
private static void Seed() { var context = new BugTrackerDbContext(); if (!context.Bugs.Any()) { context.Bugs.Add(new Bug() { Title = "Bug 1", Description = "First Bug", SubmitDate = DateTime.Now }); context.SaveChanges(); } }
public void FindWhenObjectIsInDbShouldReturnObject() { // Arrange var bug = GetValidTestBug(); var databaseContext = new BugTrackerDbContext(); var repo = new EfRepository<Bug>(databaseContext); // Act databaseContext.Bugs.Add(bug); databaseContext.SaveChanges(); var bugInDb = repo.Find(bug.Id); // Assert Assert.IsNotNull(bugInDb); Assert.AreEqual(bug.Text, bugInDb.Text); }
private static void Seed() { var context = new BugTrackerDbContext(); var comment =new Comment() { Text = "First comment", CreatedOn = DateTime.Now }; context.Comments.Add(comment); var bug = new Bug() { Title = "First bug", Description = "Bug 1", SubmitDate = DateTime.Now }; bug.Comments.Add(comment); context.Bugs.Add(bug); context.SaveChanges(); }
public void ListBugs_NonEmptyDb_ShouldReturnBugsList() { // Arrange CleanDatabase(); var dbContext = new BugTrackerDbContext(); dbContext.Bugs.Add( new Bug() { Text = "Bug #" + DateTime.Now.Ticks, DateCreated = DateTime.Now, Status = BugStatus.New }); dbContext.Bugs.Add( new Bug() { Text = "Bug #" + DateTime.Now.Ticks, DateCreated = DateTime.Now, Status = BugStatus.Fixed }); dbContext.SaveChanges(); // Act var httpResponse = httpClient.GetAsync("/api/bugs").Result; var bugsFromService = httpResponse.Content.ReadAsAsync<List<Bug>>().Result; // Assert Assert.AreEqual(HttpStatusCode.OK, httpResponse.StatusCode); Assert.AreEqual(httpResponse.Content.Headers.ContentType.MediaType, "application/json"); var bugsFromDb = dbContext.Bugs.ToList(); Assert.AreEqual(bugsFromDb.Count, bugsFromService.Count); // Assert the bugs in the DB are the same as the bugs returned from the service for (int i = 0; i < bugsFromService.Count; i++) { Assert.AreEqual(bugsFromService[i].Id, bugsFromDb[i].Id); Assert.AreEqual(bugsFromService[i].Text, bugsFromDb[i].Text); Assert.AreEqual(bugsFromService[i].Status, bugsFromDb[i].Status); Assert.AreEqual(bugsFromService[i].DateCreated.ToString(), bugsFromDb[i].DateCreated.ToString()); } }
private void CleanDatabase() { // Clean all data in all database tables var dbContext = new BugTrackerDbContext(); dbContext.Bugs.Delete(); // ... // dbContext.AnotherEntity.Delete(); // ... dbContext.SaveChanges(); }