RSpec:如何根据先前测试的结果隐式过滤测试?

发布于 2024-11-07 15:24:59 字数 744 浏览 0 评论 0原文

我正在迭代网页上的树控件。单击树中的某些节点将更改 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 技术交流群。

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

发布评论

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

评论(2

时光清浅 2024-11-14 15:24:59

测试应该始终运行。它应该设置执行您期望的代码路径所需的状态。在我看来,根据其他测试的结果有条件地执行测试完全破坏了测试的精神。

你应该已经知道以前的视图和当前的视图是不同的,如果不是你所期望的,那么你就失败了。

每个测试都应该有一个非常具体的路径,通过您期望它执行的代码,如果没有,您应该失败。没有办法做你想做的事,因为你不应该那样做。

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.

瞄了个咪的 2024-11-14 15:24:59

我不熟悉 rspec,但是您尝试过使用 Proc 吗?例如...

it "Runs only if the view is different", :if => lambda { viewDifferent? } do
  puts "Doing some stuff."
end

作为速记的符号甚至可能起作用...

it "Runs only if the view is different", :if => :viewDifferent? do
  puts "Doing some stuff."
end

正如您目前所拥有的那样,一旦声明了测试,它就会调用 viewDifferent? 方法。声明。您真正想要的是传递一个 Proc,以便在测试运行时调用它。

I'm not familiar w/ rspec, but have you tried using a Proc? For example...

it "Runs only if the view is different", :if => lambda { viewDifferent? } do
  puts "Doing some stuff."
end

A symbol as shorthand may even work...

it "Runs only if the view is different", :if => :viewDifferent? do
  puts "Doing some stuff."
end

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 a Proc so that it gets called when the test is run.

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