我可以编写哪个测试来强制执行以下代码

发布于 2024-12-12 11:19:37 字数 639 浏览 0 评论 0原文

我喜欢通过测试来强制我的设计,但当我涉及系列时,我总是遇到问题。我想要强制执行的代码如下:

clientInvoices : (client, callback)->

  @all (invoices)->

    callback invoice \
    for invoice in invoices \
    when invoice.data.clientId() is client.data._id()

起初我写道:

it 'should get randomid1 invoices', ->

  subject.clientInvoices client, (invoices)->

    invoices.length.should_be 3

但由于您需要以最少的努力使测试通过,因此您可以通过执行此操作来完成

clientInvoices : (client, callback)->

  @all (invoices)-> callback [1,2,3]

此测试 代码位于 Coffee-Script 中,但示例位于 Python、Ruby 中或 JavaScript 都受到赞赏。

I like to have my test to force my design but when I comes to collections I always run into problems. The code I want to force is the following:

clientInvoices : (client, callback)->

  @all (invoices)->

    callback invoice \
    for invoice in invoices \
    when invoice.data.clientId() is client.data._id()

At first I wrote:

it 'should get randomid1 invoices', ->

  subject.clientInvoices client, (invoices)->

    invoices.length.should_be 3

But since you need to make the test pass with the least amount of effort you could make this pass by doing

clientInvoices : (client, callback)->

  @all (invoices)-> callback [1,2,3]

The code is in Coffee-Script but examples in Python, Ruby or JavaScript are all appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待"谢繁草 2024-12-19 11:19:37

这没有意义:

for invoice in invoices \
when invoices.data.clientId() is client.data._id()

我想你的意思一定是

for invoice in invoices \
when invoice.data.clientId() is client.data._id()

如果是这样的话,那么解决你的困境的方法就是简单地添加更多关于回调收到的 invoices 对象的断言,例如

invoice.data.clientId().should_be client.data._id() for invoice in invoices

(或者任何测试中断言的正确语法 - 您在示例中使用了 should_be)。通过这个额外的断言,您可以更轻松地使函数正常工作,而不是让它生成虚假数据。

This doesn't make sense:

for invoice in invoices \
when invoices.data.clientId() is client.data._id()

I think you must mean

for invoice in invoices \
when invoice.data.clientId() is client.data._id()

If that's the case, then the solution to your quandary is simply to add more assertions about the invoices object received by the callback, e.g.

invoice.data.clientId().should_be client.data._id() for invoice in invoices

(or whatever is the correct syntax for assertions in your test—you used should_be in your example). With that extra assertion, it's easier for you to make your function work properly than to have it generate fake data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文