【问题标题】:How to write if condtional statement in jasmine javascript?如何在 jasmine javascript 中编写 if 条件语句?
【发布时间】:2017-01-31 20:54:07
【问题描述】:

我有一个 if 块条件。我需要在 jasmine javascript 中为 if 条件编写测试用例。同时参考这个网站

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

这是我在 if 块中的代码:-

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

期望:-

1.如果amp值未定义,则条件为假。

2.如果amp有值,第二条语句会被执行。

对于第二行,我可以编写 jasmine 代码

expect(a).tobe(amp)

我有点困惑,如何为 if 块实现 jasmine js。谁能给我一个简单的例子,用于 jasmine js 中的 if 条件块。

一个简单的例子可能会有所帮助吗?

谁能帮我实现这个目标?

【问题讨论】:

  • 只需创建两个不同的测试

标签: javascript unit-testing jasmine


【解决方案1】:

不确定我是否理解正确。

你可以按照那里的描述编写一个cutsom匹配器

或者你会像在

中那样做两个测试
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');
    });
});

希望对你有帮助

【讨论】:

    【解决方案2】:

    为了测试 Not null 或 Undefined ,你可以这样写。

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

    如果你想测试是否为 null 和 undefined

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

    【讨论】:

      猜你喜欢
      • 2011-08-08
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2012-05-03
      • 1970-01-01
      • 2016-08-17
      • 2015-03-22
      • 1970-01-01
      相关资源
      最近更新 更多