RSpec:如何根据先前测试的结果隐式过滤测试?
我正在迭代网页上的树控件。单击树中的某些节点将更改 FRAME_C 中的内容,单击其他节点则不会。如何过滤测试以仅在内容更改时运行?这就是我正在尝试的:
def viewDifferent?
if $prvView != $curView
return true
else
return false
end
end
...
describe "Exercising View" do
it "clicks a node in the tree control" do
$prvView = $b.frame( :id, 'FRAME_C').document.body.innertext
Timeout.timeout(50) do
spn.fire_event('onmouseup')
end
$curView = $b.frame( :id, 'FRAME_C').document.body.innertext
end
it "Runs only if the view is different", :if => viewDifferent? do
puts "Doing some stuff."
end
end
我的问题是 RSpec 在执行任何测试之前正在评估所有测试的过滤器。与上面的例子中的view有什么不同?将始终(并且确实)返回 false,因为先前的测试尚未设置两个全局变量。
有办法做我所要求的吗?几天来我一直在试图解决这个问题。
I'm iterating through a tree control on a webpage. Clicking on some nodes in the tree will change the content in FRAME_C, clicking on others will not. How do I filter a test to only run when the content has changed? Here's what I'm trying:
def viewDifferent?
if $prvView != $curView
return true
else
return false
end
end
...
describe "Exercising View" do
it "clicks a node in the tree control" do
$prvView = $b.frame( :id, 'FRAME_C').document.body.innertext
Timeout.timeout(50) do
spn.fire_event('onmouseup')
end
$curView = $b.frame( :id, 'FRAME_C').document.body.innertext
end
it "Runs only if the view is different", :if => viewDifferent? do
puts "Doing some stuff."
end
end
My problem is that RSpec is evaluating the filter for all of my tests before executing any of them. In the above example viewDifferent? will always (and does) return false since the two global variables have yet to be set by the previous test.
Is there a way to do what I'm asking? I've been trying to figure this out for days.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
测试应该始终运行。它应该设置执行您期望的代码路径所需的状态。在我看来,根据其他测试的结果有条件地执行测试完全破坏了测试的精神。
你应该已经知道以前的视图和当前的视图是不同的,如果不是你所期望的,那么你就失败了。
每个测试都应该有一个非常具体的路径,通过您期望它执行的代码,如果没有,您应该失败。没有办法做你想做的事,因为你不应该那样做。
A test should always run. It should setup the state it requires to execute the code path you expect. It seems to me that executing tests conditionally based on the outcome of other tests totally breaks the spirits of the tests.
You should already know the previous view and the current view are different, and if are not what you expect you have a failure.
Every test should have a very specific path through your code you expect it to execute, and you should fail if it doesn't. There isn't a way to do what you want because you shouldn't do it that way.
我不熟悉 rspec,但是您尝试过使用
Proc
吗?例如...作为速记的符号甚至可能起作用...
正如您目前所拥有的那样,一旦声明了测试,它就会调用
viewDifferent?
方法。声明。您真正想要的是传递一个 Proc,以便在测试运行时调用它。I'm not familiar w/ rspec, but have you tried using a
Proc
? For example...A symbol as shorthand may even work...
As you currently have it, it's calling the
viewDifferent?
method as soon as the test is declared. What you really want is to pass aProc
so that it gets called when the test is run.