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);
        }
Exemplo n.º 2
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 AddBugWhenBugIsValidShouldAddToDatabase()
        {
            // Arrange
            var bug = GetValidTestBug();

            var databaseContext = new BugTrackerDbContext();
            var repo = new EfRepository<Bug>(databaseContext) { bug };

            // Act
            repo.SaveChanges();

            var bugInDb = databaseContext.Bugs.Find(bug.Id);

            // Assert
            Assert.IsNotNull(bugInDb);
            Assert.AreEqual(bug.Text, bugInDb.Text);
        }
 public void GetBugComments_ShouldReturn200OK_ExistingBug()
 {
     var dbContext = new BugTrackerDbContext();
     var existingBug = dbContext.Bugs.FirstOrDefault(b=>b.Comments.Any());
     if (existingBug == null)
     {
         Assert.Fail("Cannot perform test - no bugs in DB.");
     }
     var endpoint = string.Format("api/bugs/{0}/comments", existingBug.Id);
     var response = client.GetAsync(endpoint).Result;
     Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
     var comments = response.Content.ReadAsAsync<List<CommentViewModel>>().Result;
     foreach (var comment in comments)
     {
         Assert.IsNotNull(comment.Text);
         Assert.AreNotEqual(0, comment.Id);
     }
 }
        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());
            }
        }
 protected BaseApiController(BugTrackerDbContext data)
 {
     this.Data = data;
 }
Exemplo n.º 11
0
 public BaseApiController(BugTrackerDbContext data)
 {
     this.Data = data;
 }
 private void CleanDatabase()
 {
     // Clean all data in all database tables
     var dbContext = new BugTrackerDbContext();
     dbContext.Bugs.Delete();
     // ...
     // dbContext.AnotherEntity.Delete();
     // ...
     dbContext.SaveChanges();
 }
        public void CreateBug_ShouldCreateBugInTheDb()
        {
            // Arrange
            CleanDatabase();
            var bugText = "Bug #" + DateTime.Now.Ticks;

            // Act
            var postContent = new FormUrlEncodedContent(new[] 
            {
                new KeyValuePair<string, string>("text", bugText)
            });
            var httpResponse = httpClient.PostAsync("/api/bugs", postContent).Result;
            var bugFromService = httpResponse.Content.ReadAsAsync<Bug>().Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.Created, httpResponse.StatusCode);
            Assert.IsNotNull(httpResponse.Headers.Location);
            Assert.IsTrue(bugFromService.Id != 0);
            Assert.AreEqual(bugFromService.Text, bugText);
            Assert.AreEqual(bugFromService.Status, BugStatus.New);
            Assert.IsNotNull(bugFromService.DateCreated);

            // Assert the database values are correct
            var dbContext = new BugTrackerDbContext();
            var bugFromDb = dbContext.Bugs.FirstOrDefault();
            Assert.IsNotNull(bugFromDb);
            Assert.AreEqual(bugFromService.Id, bugFromDb.Id);
            Assert.AreEqual(bugFromService.Text, bugFromDb.Text);
            Assert.AreEqual(bugFromService.DateCreated.ToString(), bugFromDb.DateCreated.ToString());
            Assert.AreEqual(bugFromService.Status, bugFromDb.Status);
        }