Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate fails in unit tests

I am running a unit test of my PostMyModel route. However, within PostMyModel() I used the line Validate<MyModel>(model) to revalidate my model after it is changed. I am using a test context, so as not to be dependent on the db for the unit tests. I have posted the test context and post method below:

Test 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() { }

    }

Post 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 Validate<MyModel>(model) line runs, I get the error :

System.InvalidOperationException: ApiController.Configuration must not be null.

How can I correct this?

like image 949
steventnorris Avatar asked Feb 26 '15 21:02

steventnorris


People also ask

What does a unit test validate?

Unit Testing is a type of software testing where individual units or components of a software are tested. The purpose is to validate that each unit of the software code performs as expected. Unit Testing is done during the development (coding phase) of an application by the developers.

What makes a unit test self-validating?

Self-validating means that a test should perform operations and programmatically check for the result. For instance, if you're testing that you've written something on a file, the test itself is in charge of checking that it worked correctly. No manual operations should be done.

What are the errors in unit testing?

Unit testing considerations What errors are commonly found during Unit Testing? (1) Misunderstood or incorrect arithmetic precedence, (2) Mixed mode operations, (3) Incorrect initialization, (4) Precision inaccuracy, (5) Incorrect symbolic representation of an expression.


1 Answers

In order for the Validate command to run, there must be mock HttpRequest associated with the controller. The code to do this is below. This will mock a default HttpRequest, which is fairly unused in this case, allowing the method to be unit tested.

 HttpConfiguration configuration = new HttpConfiguration();
            HttpRequestMessage request = new HttpRequestMessage();
            controller.Request = request;
            controller.Request.Properties["MS_HttpConfiguration"] = configuration;
like image 161
steventnorris Avatar answered Nov 01 '22 18:11

steventnorris