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 void PostBug_WhenBugTextIsValid_ShouldBeAddedToRepoWithDateCreatedAndStatusNew()
        {
            // Arrange
            var repo = new RepositoryMock<Bug>();
            repo.IsSaveCalled = false;
            repo.Entities = new List<Bug>();
            var newBug = new Bug()
            {
                Text = "Test bug"
            };
            var controller = new BugsController(repo);
            this.SetupController(controller, "bugs");

            // Act
            var httpResponse = controller.PostBug(newBug).ExecuteAsync(new CancellationToken()).Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.Created, httpResponse.StatusCode);
            Assert.IsNotNull(httpResponse.Headers.Location);

            var bugFromService = httpResponse.Content.ReadAsAsync<Bug>().Result;
            Assert.AreEqual(bugFromService.Text, newBug.Text);
            Assert.AreEqual(bugFromService.Status, BugStatus.New);
            Assert.IsNotNull(bugFromService.DateCreated);

            // Assert the repository values are correct
            Assert.AreEqual(repo.Entities.Count, 1);
            var bugInRepo = repo.Entities.First();
            Assert.AreEqual(newBug.Text, bugInRepo.Text);
            Assert.IsNotNull(bugInRepo.DateCreated);
            Assert.AreEqual(BugStatus.New, bugInRepo.Status);
            Assert.IsTrue(repo.IsSaveCalled);
        }
        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
        }
        public IHttpActionResult PostBug(Bug bug)
        {
            if (string.IsNullOrEmpty(bug.Text))
            {
                return this.BadRequest("Text cannot be null");
            }

            bug.Status = BugStatus.New;
            bug.DateCreated = DateTime.Now;
            this.repo.Add(bug);
            this.repo.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = bug.Id }, bug);
        }
        public void AddBug_WhenBugIsAddedToDb_ShouldReturnBug()
        {
            // Arrange -> prapare the objects
            var repo = new BugsRepository(new BugTrackerDbContext());
            var bug = new Bug()
            {
                Text = "Sample New Bug",
                DateCreated = DateTime.Now,
                Status = BugStatus.New
            };

            // Act -> perform some logic
            repo.Add(bug);
            repo.SaveChanges();

            // Assert -> validate the results
            var bugFromDb = repo.Find(bug.Id);

            Assert.IsNotNull(bugFromDb);
            Assert.AreEqual(bug.Text, bugFromDb.Text);
            Assert.AreEqual(bug.Status, bugFromDb.Status);
            Assert.AreEqual(bug.DateCreated, bugFromDb.DateCreated);
            Assert.IsTrue(bugFromDb.Id != 0);
        }
        public void PostBug_WhenBugTextIsInvalid_ShouldReturnBadRequest()
        {
            // Arrange
            var repo = new RepositoryMock<Bug>();
            var controller = new BugsController(repo);
            this.SetupController(controller, "bugs");

            // Act
            var newBug = new Bug() { Text = null };
            var result = controller.PostBug(newBug).ExecuteAsync(new CancellationToken()).Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.BadRequest, result.StatusCode);
        }
        public void PostBug_WhenValid_ShouldAddBugToDb_WithMoq()
        {
            // Arrange
            var repoMock = new Mock<IRepository<Bug>>();

            List<Bug> bugs = new List<Bug>()
            {
                new Bug() { Text = "Test bug #1" },
                new Bug() { Text = "Test bug #2" }
            };

            repoMock.Setup(repo => repo.Add(It.IsAny<Bug>()))
                .Callback((Bug b) => bugs.Add(b));

            var controller = new BugsController(repoMock.Object);
            this.SetupController(controller, "bugs");

            // Act
            var newBug = new Bug() { Text = "new bug" };
            var result = controller.PostBug(newBug).ExecuteAsync(new CancellationToken()).Result;

            // Assert
            Assert.AreEqual(HttpStatusCode.Created, result.StatusCode);
            Assert.AreEqual(newBug.Text, bugs.Last().Text);
            Assert.AreEqual(BugStatus.New, bugs.Last().Status);
            Assert.IsNotNull(bugs.Last().DateCreated);
        }