public IHttpActionResult PostBug(SaveBugModel bug) { if (bug == null) { var ex = new ArgumentException("No data sent!"); return(this.BadRequest(ex.Message)); } if (!this.ModelState.IsValid) { return(this.BadRequest(this.ModelState)); } if (string.IsNullOrEmpty(bug.Text)) { var ex = new ArgumentException("Text of the bug cannot be null or empty."); return(this.BadRequest(ex.Message)); } Bug model = Mapper.Map(bug, new Bug()); this.repo.Add(model); this.repo.Save(); var returnModel = Mapper.Map(model, new ResponseBugModel()); return(this.Created(new Uri("http://localhost:11350/api/bugs"), returnModel)); }
public void TestPostingANewBugWithOutTextParameterProvidedToReturnBadRequest() { var bugToPost = new SaveBugModel(); MyWebApi .Controller <BugsController>() .Calling(c => c.PostBug(bugToPost)) .ShouldReturn() .BadRequest() .WithModelStateFor <SaveBugModel>() .ContainingModelStateErrorFor(err => err.Text).ThatEquals("Text of the bug cannot be null or empty."); }
public IHttpActionResult PutBug(int id, [FromBody] SaveBugModel updatedBug) { Bug bug = this.repo.Find(id); if (bug == null) { return(this.BadRequest("Id not found")); } Mapper.DynamicMap(updatedBug, bug); this.repo.Update(bug); this.repo.Save(); return(this.Ok("Changes Saved!")); }
public void TestPostingANewBugWithOnlyTextParameterProvided() { var bugToPost = new SaveBugModel(); bugToPost.Text = "TestLoggin BugData"; MyWebApi .Controller <BugsController>() .WithResolvedDependencyFor <IRepository <Bug> >(this.repo) .Calling(c => c.PostBug(bugToPost)) .ShouldReturn() .Created() .WithResponseModelOfType <ResponseBugModel>() .Passing(c => c.Text == bugToPost.Text); }
public void TestPostingANewBugWithFullParametersProvided() { var bugToPost = new SaveBugModel(); bugToPost.Text = "AssignedToPesho"; bugToPost.LogDate = new DateTime(2013, 3, 3); bugToPost.Status = SaveStatus.Assigned; MyWebApi .Controller <BugsController>() .Calling(c => c.PostBug(bugToPost)) .ShouldReturn() .Created() .WithResponseModelOfType <ResponseBugModel>() .Passing(c => { return(c.Text == bugToPost.Text && c.LogDate == bugToPost.LogDate.Value.Day + "." + bugToPost.LogDate.Value.Month + "." + bugToPost.LogDate.Value.Year && c.Status == "Assigned"); }); }
public void AddBug_WhenBugTextIsValid_ShouldBeAddedToRepoWithLogDateAndStatusPending() { var repo = new FakeRepository <Bug>(); repo.IsSaveCalled = false; repo.Entities = new List <Bug>(); var bug = new SaveBugModel() { Text = "NEW TEST BUG" }; var controller = new BugsController(repo); this.SetupController(controller); controller.PostBug(bug); Assert.AreEqual(repo.Entities.Count, 1); var bugInDb = repo.Entities.First(); Assert.AreEqual(bug.Text, bugInDb.Text); Assert.IsNotNull(bugInDb.LogDate); Assert.AreEqual(Status.Pending, bugInDb.Status); Assert.IsTrue(repo.IsSaveCalled); }