在视图测试中,何时使用before(:all)?

时间:2015-10-30 16:33:44

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

我在应用程序中编写了一些视图测试,我的测试期望如下:

Dim v As Variant
v = [INDEX(D8:F10, 2, 0)]

但是我和之前(:每个)在应用程序上迭代太多请求,使我的测试花费25秒来运行。我已经改为之前(:全部),然后变为4秒。

  • 我还应该使用之前(:每个)?
  • 为什么之前(:all)不推荐?

编辑:我之前的迭代:

describe 'form' do
  it 'has a search form' do
    render
    expect(rendered).to have_selector 'form[id=mock_search]'
  end
  it 'has a name filter' do
    render
    expect(rendered).to have_selector 'label[for=q_name_cont]', text: 'Nome do simulado'
    expect(rendered).to have_selector 'input[id=q_name_cont]'
  end
  it 'has a submit button' do
    render
    expect(rendered).to have_selector 'input[type=submit][value="Buscar"][name=commit]'
  end
  it 'has a reset button' do
    render
    expect(rendered).to have_selector 'input[type=submit][value="Limpar filtros"]'
  end
end

2 个答案:

答案 0 :(得分:2)

describe 'form' do
  # 
  #let! will create create instance variable school 
  #inside your test case and assign your school build. Different between 
  #let! and let is: let! create before every task. You won't need it to put 
  #on before block and let doesn't do that you have to call it explicitly to 
  #create that mock and get values..   
  let!(:school) { build(:school) }
  let!(:teacher) { build(:teacher) }
  let!(:statistics) { build_list(:mock_with_proccessed_statistics, 2, school: @school, teacher: @teacher) }
  let!(:mocks) { Mock.page(nil) }

  subject { render }

  it 'has a search form' do
    expect{ subject }.to have_selector 'form[id=mock_search]'
  end

  it 'has a name filter' do
    expect{ subject }.to have_selector 'label[for=q_name_cont]', text: 'Nome do simulado'
    expect{ subject }.to have_selector 'input[id=q_name_cont]'
  end
end

答案 1 :(得分:0)

before(:each)在每个测试用例执行之前执行。和before(:all)在当前上下文中的所有测试用例之前。