Testing Model is valid

Question!

Working on a web app, MVC 5 in VS 2015.

Here is my model:

public class InfoFormulaireEmployeModele
{
    [Required(ErrorMessage =" *You must provide a date")]
    [RegularExpression("^[0-9]{8}$", ErrorMessage ="The date must be of the format AAAAMMJJ")]
    public string dateEvenementInitial { get; set; }
}

When I try to enter some invalid input, let's say '12ab' , it works fine, I'm back on the page with the error message.

Here is my view:

@using PortailLibreService.Models
@model InfoFormulaireEmployeModele
@{
    ViewBag.Title = "ChampsFormulaireInvalidite";
}

@using (Html.BeginForm())
{
    <div class="editor-field">
        <b>Initial event </b><br>
        <p>When did the initial event occured?</p>
         @Html.TextBoxFor(x => x.dateEvenementInitial, new { @placeholder = "AAAAMMJJ" })  @Html.ValidationMessageFor(x => x.dateEvenementInitial, null, new { @class = "error" })
    </div>
    <input type="submit" name="Save" value="Submit" />
}

I wanted to create a unit test to be sure that my model wouldn't be valid with an invalid date entry.

Here is my unit test:

[TestMethod]
public void testRetourPossibleInvalide()
{
    InfoFormulaireEmployeModele modelDuplicat = new    InfoFormulaireEmployeModele();

        modelDuplicat.dateEvenementInitial = "abc";


        InvFormController controlleur = lib.getController("100237");
        var result = controlleur.ChampsFormulaireInvalidite(modelDuplicat);
        Assert.AreEqual(false, controlleur.ModelState.IsValid);
}

For some reason the test fails, controlleur.ModelState.IsValid is true while it should be false. If I test it with only numerical values it's fine. For example, this test passed:

[TestMethod]
public void testRetourPossibleInvalide()
{
    InfoFormulaireEmployeModele modelDuplicat = new        InfoFormulaireEmployeModele();

    modelDuplicat.dateRetourPossible = "129";
    InvFormController controlleur = lib.getController("100237");
    var result = controlleur.ChampsFormulaireInvalidite(modelDuplicat);
    Assert.AreEqual(false, controlleur.ModelState.IsValid);
}

controlleur.ModelState.IsValid is false while running the test, so the test passes.

I tried to put a breakpoint inside my controller method but I can't reach it while debugging..



Answers

You would have to act as the framework, which usually does all the checking for you.

[TestMethod]
public void testRetourPossibleInvalide() {
    var model = new InfoFormulaireEmployeModele();

    model.dateEvenementInitial = "abc";

    var controller = new InvFormController();

    var bindingContext = new ModelBindingContext() {
        ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => model, model.GetType()),
        ValueProvider = new NameValueCollectionValueProvider(new NameValueCollection(), CultureInfo.InvariantCulture)
    };
    var boundModel = new DefaultModelBinder().BindModel(new ControllerContext(), bindingContext);

    controller.ModelState.Clear();
    controller.ModelState.Merge(bindingContext.ModelState);

    var result = controller.ChampsFormulaireInvalidite(model);

    Assert.AreEqual(false, controller.ModelState.IsValid);
}
By : Nkosi


You could do some simple reflection and get the folder the executing assembly is in, then reference that file in that location. I did this successfully for an ancillary file I needed that is deployed to the same folder as my executable.

string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string filePath = Path.Combine(Path.GetDirectoryName(exePath), "myfilename.txt");
By : BrettRobi


You can select build action as 'content' and select 'copy if newer' in the properties so that it gets copied to the output folder.

By : charisk


This video can help you solving your question :)
By: admin