views:

39

answers:

0

Hi,

I'm trying to unit test a controller action in which I call tryupdatemodel, but the validation does not fire. When I run my application the validation occurs and is fine.

My guess is the the model binder is not loaded in test scenarios, how can I test my action ?

here is the code

Company object

 public partial class Company : IDataErrorInfo
    {
        public string Error
        {
            get { return ""; }
        }

        public string this[string columnName]
        {
            get
            {
                switch (columnName.ToUpperInvariant())
                {
                    case "NAME":
                        if (string.IsNullOrEmpty(Name))
                            return Translate("MissingCompanyName");
                        break;
...

controller action

[CaptchaValidator]
[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]
public ActionResult Register(bool captchaValid, FormCollection form)
{
    Company c = new Company();
    TryUpdateModel<Company>(c, "Company");            
...

test

[TestMethod]
        public void Register_Post()
        {
            // Arrange
            AccountController controller = new AccountController();
            MvcMockHelpers.SetFakeControllerContext(controller);

            bool captchaValid = true;
            FormCollection form = new FormCollection();
            form.Add("Company.Name", "test_cie");
            form.Add("Company.Phone", "");
            ...

            //// Act
            ViewResult result = controller.Register(captchaValid, form) as ViewResult;

the question is why the validation is not firing? and if it's a model binder not being loaded how to load it ?

thanks