How to write if condtional statement in jasmine javascript?

17,294

Solution 1

For testing Not null or Undefined , you can write like this.

describe("Your Test Controller",() => {
    it("check for not null or undefined", function (){
        expect(Function(params)).not.toBeNull();
        expect(Function(params)).not.toBeUndefined();
    });
   } 

and if you want to test for being null and undefined

describe("Your Test Controller",() => {
        it("check for null or undefined", function (){
            expect(Function(params)).toBeNull();
            expect(Function(params)).toBeUndefined();
        });
       } 

Solution 2

not sure if I understood correct.

you could write a cutsom matcher as described there

or you would do two tests as in

describe("Test amp", function() {
    it("cechk amp to be undef", function (){
        expect(yourFunction('test')).toBe(undef);
    });
    it("gives true if number", function (){
        expect(yourFunction('test2')).toBe('your Testresult');
    });
});

hope it helps

Share:
17,294
Thiru Arasu
Author by

Thiru Arasu

Love to Do Something Different

Updated on June 26, 2022

Comments

  • Thiru Arasu
    Thiru Arasu almost 2 years

    I have a if block condition. I need to write test case for that if condition in jasmine javascript.While referring this website

    https://jasmine.github.io/2.1/custom_matcher.html

    This is my code in if block:-

    if(!isNullorUndefined(amp))
    var a=amp;
    

    Expectation:-

    1.If the amp value is undefined,the condition is to be false.

    2.If the amp had value,the second statement gonna to be executed.

    for second line,i can able to write jasmine code

    expect(a).tobe(amp)
    

    I am little confused,how to implement the jasmine js for if blocks.Could you anyone provide me an simple example for if condition block in jasmine js.

    A simple example is might be helpful?

    Could anyone help me to achieve this?