【问题标题】:What does NotStrictEqual do in xUnit?NotStrictEqual 在 xUnit 中做了什么?
【发布时间】:2018-09-13 07:30:39
【问题描述】:

NotStrictEqual 在下面代码中的 xUnit 中做了什么

// Act
var response = await _client.GetAsync("/api/articles");

// Assert
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
var articles = JsonConvert.DeserializeObject<Article[]>(responseString);
Assert.NotStrictEqual(PredefinedData.Articles, articles);

【问题讨论】:

    标签: .net unit-testing xunit.net


    【解决方案1】:

    “不严格等于”意味着您要测试两个对象的状态相等性的否定,而不管它们的参数类型。例如,在非严格模式下,比较 "1" 和 1 将被评估为 true。

    由于您没有发布文章类型的类型定义,我在下面做了一些假设。此外,由于我们正在比较自定义类型,因此最好尝试notDeepStrictEqual。如果有帮助,请参阅下文。

    describe('Test articles', function () {
    
       const PredefinedData = {
            Articles: [
                {
                    name: 'article1'
                }
            ]
       }
    
        it('this will pass', function () {
            const mockResponse = '[{"name": "article2"}]'
            const articles = JSON.parse(mockResponse);
            assert.notDeepStrictEqual(PredefinedData.Articles, articles);
        });
    
        it('this will fail', function () {
            const mockResponse = '[{"name": "article1"}]'
            const articles = JSON.parse(mockResponse);
            assert.notDeepStrictEqual(PredefinedData.Articles, articles);
        });
    
      });
    

    【讨论】:

    • 在上面的示例文章数等于 1 但 PredefinedData.Articles 数等于 2。它通过了。这正常吗?
    • 听起来不错。你也可以测试它吗?使文章计数和predefinedData.articles 计数相等。然后看看你的测试用例是否通过。
    • 我在PredefinedData.Articlesarticles 中使用不同的记录(不同的值)进行了测试,但它通过了。为什么?
    • 嗨@unosbaghaii,我已经更新了我的答案。你能在你的代码中尝试类似的东西吗?
    • 我的示例代码在 .net core 2 (c#) 中。你的代码是javascript。这个测试可靠吗?
    猜你喜欢
    • 2015-11-22
    • 2014-08-03
    • 2012-02-12
    • 2010-10-26
    • 2019-07-12
    • 2010-11-20
    • 2011-07-24
    • 2012-08-07
    • 1970-01-01
    相关资源
    最近更新 更多