开发者

Qunit testing with no passed Vars

I am trying to make a test of a function that does some pretty simple validation for a form. However I cannot figure out how to use qunit without passing vars to the function. here is an example of what I am talking about

  function validateForm(){
        var name = $('#name').val();
        var submit = true;
        //do some validation
        //if submit true
        form.submit();
   }

all the example of qunit I see in the docs do something like this

   ok(validateForm('hello'),'Hello is a valid name');

should I just modify my function or is there a w开发者_如何转开发ay to work with such a setup.

//EDIT

The option I have chosen to pursue right now is setting up another function above the current one that simply parses the form inputs and then sends them as vars to the validateForm() function


What I ended up doing was modifying the function in a way that allowed me to pass the vars in the test.

before the code would do something like this

       function validateForm(){
               var value1 = $('#someinput').val();
               //Do some validation based on these vars
               $('#someform').submit();
        }

Now When the submit button is pressed it goes to another function that parses the args out for me also added a 'test' arg so the form is not being submitted during the tests.

     function parseFormArgs(){
             var value1 = $('#someinput').val();
             var value2 = $('#otherinput').val();
             var test = false;

             validateForm(value1,value2,test);
     }

     function validateForm(value1,value2,test){
               var submit = true;
               //Do some validation
               if( test != true && submit ){
                   $('#someform').submit();
               }

              return submit;
    }

Then my qunit looks something like this

      var testFn = true;
      test('validateForm()', function(){
           equals(validateForm('ExampleVal1','ExampleVal2',testFn), false, 'Such and Such Values should not validate');
           ...
           ...
       });


Does qUnit have access to the form/UI? I've only used JsTestDriver for JavaScript testing, but there you only test the internal object logic

so instead of trying to read the form (which I don't know if you can do), what you could do is get the current values from the form, and pass those to the validation function, eg:

function validate(options){

      return options.foo === "bar"

}

so, you could write a qUnit test that did something like

var options = {foo:"bar"}
var valid = validate(options)
equal(true, valid)

or

   var options = {foo:"booboo"}
    var valid = validate(options)
    equal(false, valid)

something like that? Then once you're happy with the function, you can add it into the UI

??

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜