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

时间:2011-09-10 19:13:34

标签: ruby-on-rails ruby-on-rails-3 rspec rspec2

现在我的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

有没有办法可以在每个描述块之前定义一个?像这样:

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

由于

1 个答案:

答案 0 :(得分:2)

您提供的解决方案应该有效。你试过吗?