Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 多重';在'之前;微型测试中的块';描述';块_Ruby On Rails_Ruby_Minitest - Fatal编程技术网

Ruby on rails 多重';在'之前;微型测试中的块';描述';块

Ruby on rails 多重';在'之前;微型测试中的块';描述';块,ruby-on-rails,ruby,minitest,Ruby On Rails,Ruby,Minitest,我试图在一个descripe块下有两个“before”块,但只有后者运行 describe '#bunch_of_tests' do before(:all) do p 'do this before all tests' end before(:each) do p 'do this before each test' end describe 'this is 1st test' do it 'runs 1st test' do end end

我试图在一个descripe块下有两个“before”块,但只有后者运行

describe '#bunch_of_tests' do

  before(:all) do
    p 'do this before all tests'
  end

  before(:each) do
    p 'do this before each test'
  end

describe 'this is 1st test' do
  it 'runs 1st test' do
  end
end

describe 'this is 2nd test' do
  it 'runs 2nd test' do
  end
end

end
问题是这句话永远不会被打印出来: “在所有测试之前执行此操作”


我的期望是,这应该在所有测试之前运行一次。”before(:each)块按预期工作。

这可能很笨拙,但您可以尝试:

RSpec.describe '#bunch_of_tests' do

  before(:all) do
    p 'do this before all tests'
  end

  describe "before each" do 
    before(:each) do
      p 'do this before each test'
    end

    describe 'this is 1st test' do
      it 'runs 1st test' do
      end
    end

    describe 'this is 2nd test' do
      it 'runs 2nd test' do
      end
    end
  end

end
这将产生:

#bunch_of_tests
"do this before all tests"
  before each
    this is 1st test
"do this before each test"
      runs 1st test
    this is 2nd test
"do this before each test"
      runs 2nd test

Finished in 0.40184 seconds (files took 3.5 seconds to load)
2 examples, 0 failures

我正在使用迷你测试。当我尝试你的方法时,它会在每次测试后打印:“在所有测试之前执行此操作”,而根本不会打印“在每次测试之前执行此操作”。我很抱歉,你在标题中绝对声明了Minitest,我只是错过了它。作为忏悔,我快速搜索了一下。你看了吗?这似乎与你的问题有关。谢谢。是的,这就是我开始的地方,但他们没有任何关于我尝试做什么的例子,尽管从定义上看这似乎是直观的。