简体   繁体   English

如何在softAssertAll上应用expect或在softAssertAll通过的测试结果中明确显示(错误:预期对象是一个函数)

[英]How to apply expect over softAssertAll or show explicity in the test result that the softAssertAll passed (Error: Expected Object to be a function)

I'm using 'soft-assert' library ( soft-assert library ) to apply assertion on my test steps without stopping the test if any of them fail.我正在使用“soft-assert”库( soft-assert library )在我的测试步骤上应用断言,如果其中任何一个失败,则不会停止测试。

According to the documentation, all soft-assert is verified at the end of the test by using softAssertAll() command.根据文档,在测试结束时使用 softAssertAll() 命令验证所有软断言。 And this works very well.这很好用。 However, if nothing fails, I don't see any explicit message in my test result as when I use expect command.但是,如果没有失败,我在测试结果中看不到任何明确的消息,就像我使用 expect 命令时一样。

So, I'm trying to apply expect over the softAssertAll() command, as it's seen below, but I'm getting the error message: "expected { Object (userInvocationStack, specWindow, ...) } to be a function"所以,我试图在 softAssertAll() 命令上应用 expect,如下所示,但我收到错误消息: “expected { Object (userInvocationStack, specWindow, ...) } to be a function”

What I'm trying to do:我正在尝试做的事情:

 expect(cy.softAssertAll()).not.throw(Error)

Does anyone know how can I do this or solve the error in the image below?有谁知道我该怎么做或解决下图中的错误? Thanks in advance.提前致谢。

在此处输入图像描述

See the Chai example for throw请参阅 Chai 示例中的throw

var badFn = function () { throw new TypeError('Illegal salmon!'); };
expect(badFn).to.throw();

Note you pass in the function name without invoking it.请注意,您传入函数名称而不调用它。 I think this allows chai to wrap the function invocation in a try-catch and gracefully report the failure.我认为这允许 chai 将函数调用包装在 try-catch 中并优雅地报告失败。

You can't do the same with a Cypress custom command as it will not raise errors in the same way as badFn above.您不能对 Cypress 自定义命令执行相同操作,因为它不会像上面的badFn那样引发错误。 Internally it swallows any error and sets the state of the test to "failed".在内部,它会吞下任何错误并将测试状态设置为“失败”。

You could reasonably expect this to work您可以合理地期望这会起作用

expect(jsonAssertion.softAssertAll).not.throw()

however there's an internal error in jsonAssertion that seems to be related to the this reference inside it's class.但是jsonAssertion中有一个内部错误,似乎与它的类中的this引用有关。

TypeError: Cannot read properties of undefined (reading '_softThrowJsonDiffArray') TypeError:无法读取未定义的属性(读取“_softThrowJsonDiffArray”)

To fix, use an arrow function要修复,请使用箭头函数

const testSoftAssertAll = () => jsonAssertion.softAssertAll();
expect(testSoftAssertAll).not.throw()

or shorter或更短

expect(() => jsonAssertion.softAssertAll()).not.throw()

Check the diff array检查差异数组

This is cleaner and clearer这更干净更清晰

// expect all soft-assert to be passing
expect(jsonAssertion.jsonDiffArray).to.eq(undefined)

// expect first soft-assert to fail with a certain message
expect(jsonAssertion.jsonDiffArray[0].error.message).to.contain('Illegal salmon')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM