何时在expect Rspec方法中使用花括号与括号? [英] When to use curly braces vs parenthesis in expect Rspec method?

查看:82
本文介绍了何时在expect Rspec方法中使用花括号与括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个测试:

expect(@parser.parse('adsadasdas')).to raise_error(Errno::ENOENT)

它没有用.我改为:

expect { @parser.parse('adsadasdas') }.to raise_error(Errno::ENOENT)

它奏效了.

什么时候用花括号,什么时候用括号呢?

When do we use curly braces and when do we use parentheses with expect?

推荐答案

为了回应 OP 的评论,我已经编辑并完全重写了我的答案.我意识到我原来的答案过于简单,以至于可能被认为是不正确的.

In response to OP's comment, I've edited and completely rewritten my answer. I realize that my original answer was oversimplified, so much so that it could be considered incorrect.

您的问题实际上已由另一个 StackOverflow 问题解决.

Your question was actually addressed somewhat by this other StackOverflow question.

一张海报,Peter Alfvin,他说:

至于规则,如果您要测试,则传递块或 Proc行为(例如引发错误,更改某些值).否则,你传递常规"参数,在这种情况下,该参数的值论据是被测试的东西.

As for rules, you pass a block or a Proc if you're trying to test behavior (e.g. raising errors, changing some value). Otherwise, you pass a "conventional" argument, in which case the value of that argument is what is tested.

您遇到所看到的现象的原因与出现错误有关.当您将 @parser.parse('adsadasdas') 作为参数(使用括号)传递给 expect 时,您实际上是在告诉 ruby​​:

The reason you're encountering the phenomenon you're seeing has to do with the raising of errors. When you pass @parser.parse('adsadasdas') as an argument (use parentheses) to expect, you are essentially telling ruby:

  1. 首先评估 @parser.parse('adsadasdas').
  2. 获取结果并将其传递给 expect.
  3. expect 应该看看这个结果是否符合我的预期(也就是说,Errno:ENOENT 会被提出).
  1. Evaluate @parser.parse('adsadasdas') first.
  2. Take the result and pass this to expect.
  3. expect should see if this result matches my expectation (that is, that Errno:ENOENT will be raised).

但是,发生的情况是:当 ruby​​ 对 @parser.parse('adsadasdas') 求值时,就会立即引发错误.Ruby 甚至没有机会将结果传递给 expect.(就我们而言,您可以将 @parser.parse('adsadasdas') 作为参数传递给任何函数……例如 multiply()capitalize()) 引发错误,expect 甚至没有机会完成它的工作.

But, what happens is: when ruby evaluates @parser.parse('adsadasdas'), an error is raised right then and there. Ruby doesn't even get a chance to pass the result on to expect. (For all we care, you could have passed @parser.parse('adsadasdas') as an argument to any function... like multiply() or capitalize()) The error is raised, and expect never even gets a chance to do its work.

但是当您使用花括号将 @parser.parse('adsadasdas') 作为 proc(代码块)传递给 expect 时,您告诉 ruby​​ 的是这个:

But when you pass @parser.parse('adsadasdas') as a proc (a code block) to expect using curly braces, what you are telling ruby is this:

  1. expect,准备做一些工作.
  2. expect,我希望您在我们评估 @parser.parse('adsadasdas') 时跟踪发生的情况.
  3. 好的,expect,刚刚评估的代码块是否引发了 Errno:ENOENT 错误?我期待它会.
  1. expect, get ready to do some work.
  2. expect, I would like you to keep track of what happens as we evaluate @parser.parse('adsadasdas').
  3. Ok, expect, did the code block that was just evaluated raise a Errno:ENOENT error? I was expecting that it would.

当您将代码块传递给 expect 时,您是在告诉 expect 您希望它检查结果行为,即代码块执行所做的更改,然后让您知道它是否符合您提供的期望.

When you pass a code block to expect, you are telling expect that you want it to examine the resulting behavior, the changes, made by your code block's execution, and then to let you know if it meets up to the expectations that you provide it.

当您将参数传递给 expect 时,您是在告诉 ruby 评估该参数以获得某个值 before expect 甚至参与其中,然后您将该值传递给 expect 以查看它是否符合某些期望.

When you pass an argument to expect, you are telling ruby to evaluate that argument to come to some value before expect even gets involved, and then you are passing that value to expect to see if it meets up to some expectation.

这篇关于何时在expect Rspec方法中使用花括号与括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆