I run the unit test of my PostMyModel route. However, in PostMyModel() I used the Validate<MyModel>(model) to re-validate my model after changing it. I use a test context to not be db dependent for unit tests. I placed the test context and submit method below:
Testing context
class TestAppContext : APIContextInterface { public DbSet<MyModel> MyModel { get; set; } public TestAppContext() { this.MyModels = new TestMyModelDbSet(); } public int SaveChanges(){ return 0; } public void MarkAsModified(Object item) { } public void Dispose() { } }
Shipping method
[Route(""), ResponseType(typeof(MyModel))] public IHttpActionResult PostMyModel(MyModel model) { //Save model in DB model.status = "Waiting"; ModelState.Clear(); Validate<MyModel>(model); if (!ModelState.IsValid) { return BadRequest(ModelState); } db.MyModels.Add(model); try { db.SaveChanges(); } catch (DbUpdateException) { if (MyModelExists(model.id)) { return Conflict(); } else { throw; } } return CreatedAtRoute("DisplayMyModel", new { id = model.id }, model); }
When the line Validate<MyModel>(model) is executed, I get an error message:
System.InvalidOperationException: ApiController.Configuration must not be null.
How can i fix this?
source share