RSpec测试一系列动作

发布于 2024-12-04 11:59:27 字数 756 浏览 0 评论 0原文

我有一个复杂的交易流程,我想使用 RSpec 进行测试。我想在每个步骤之后进行一些检查。我只知道如何在单独的操作中测试它们。因此,在每个步骤中,我必须添加在前面的步骤中已指定的操作,如下所示:

it "should add money to account A through deposit"
  a.deposit(10)
  a.balance.should == 10
end

it "should subtract money from A through transfer"
  a.deposit(10)
  a.transfer b, 5
  a.balance.should == 5
  b.balance.should == 5
end

it "should reverse transaction through reverse"
  a.deposit(10)
  a.transfer b, 5
  a.reserve b, 5
  a.balance.should == 10
  b. balance.should == 10
end

我想要做的是:

it "should perform a series of actions successfully"
  a.deposit(10)
   # checking here
  a.transfer b, 5
    # checking here
  a.reserve b, 5
    # checking here  
end

可以这样做吗?

谢谢

I have a complex transaction process I want to test using RSpec. I would like to have some checking after each step. I only know how to test them in separate actions. So in each step, I have to add actions I already specified in previous steps as follow:

it "should add money to account A through deposit"
  a.deposit(10)
  a.balance.should == 10
end

it "should subtract money from A through transfer"
  a.deposit(10)
  a.transfer b, 5
  a.balance.should == 5
  b.balance.should == 5
end

it "should reverse transaction through reverse"
  a.deposit(10)
  a.transfer b, 5
  a.reserve b, 5
  a.balance.should == 10
  b. balance.should == 10
end

What I want to do is:

it "should perform a series of actions successfully"
  a.deposit(10)
   # checking here
  a.transfer b, 5
    # checking here
  a.reserve b, 5
    # checking here  
end

Is it possible to do?

Thank you

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

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

发布评论

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

评论(1

烂人 2024-12-11 11:59:28

您最初的测试实际上更好,因为如果失败,您将更好地了解问题出在哪里。如果您想减少每个测试之间的重复,请使用 before(:each)

Your original tests are actually better, because if one fails you'll have a better understanding of where the problem was. If you want to reduce the duplication between each test, use before(:each)

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