简体   繁体   English

使用RSpec,有没有办法为描述块定义before(:each)?

[英]With RSpec, is there a way to define a before(:each) for a describe block?

Right now my rpsec goes like this: 现在我的rpsec是这样的:

describe Ability do

  before(:each) do
  end

  describe 'xxx' do
    it "should xxx" do
    end
    it "should xxx" do
    end
  end

  describe 'xxx' do
    it "should xxx" do
    end
    it "should xxx" do
    end
  end

  describe 'xxx' do
    it "should xxx" do
    end
    it "should xxx" do
    end
  end

  describe 'xxx' do
    it "should xxx" do
    end
    it "should xxx" do
    end
  end

end

Is there a way I can define a before each for just one of the describe blocks? 有没有一种方法可以在每个描述块之前定义一个? Something like this: 像这样的东西:

describe Ability do

  before(:each) do
  end

  describe 'xxx' do
    it "should xxx" do
    end
    it "should xxx" do
    end
  end

  describe 'xxx' do
    it "should xxx" do
    end
    it "should xxx" do
    end
  end

  describe 'xxx' do
    it "should xxx" do
    end
    it "should xxx" do
    end
  end

  describe 'xxx' do

    before(:each) do
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    end

    it "should xxx" do
    end
    it "should xxx" do
    end
  end

end

Thanks 谢谢

The solution you gave should work. 你给出的解决方案应该有效。 Have you tried it ? 你试过吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM