为什么第一个Rspec View测试总是运行得最慢?

时间:2016-07-29 15:08:40

标签: ruby-on-rails ruby testing rspec

下面是我的Rspec输出,你可以看到第一个要运行的测试总是最慢的。第一次是第18行,第二次是第27行。测试可以进行任何类型的缓存吗?

❯ spec spec/views/principals/show.html.haml_spec.rb -p                                                            mbc
Run options:
  include {:focus=>true}
  exclude {:ignore=>true}

All examples were filtered out; ignoring {:focus=>true}

Randomized with seed 25466
..

Top 2 slowest examples (3.47 seconds, 94.4% of total time):
  /principals/show the MINDEX tab a permitted user has tab and a link to the tab
    3.41 seconds ./spec/views/principals/show.html.haml_spec.rb:18
  /principals/show the MINDEX tab a not-permitted user does not have tab or a link to the tab
    0.05673 seconds ./spec/views/principals/show.html.haml_spec.rb:27

Finished in 3.68 seconds (files took 5.98 seconds to load)
2 examples, 0 failures

Randomized with seed 25466

❯ rspec spec/views/principals/show.html.haml_spec.rb -p                                                            mbc
Run options:
  include {:focus=>true}
  exclude {:ignore=>true}

All examples were filtered out; ignoring {:focus=>true}

Randomized with seed 33681
..

Top 2 slowest examples (3.98 seconds, 94.9% of total time):
  /principals/show the MINDEX tab a not-permitted user does not have tab or a link to the tab
    3.73 seconds ./spec/views/principals/show.html.haml_spec.rb:27
  /principals/show the MINDEX tab a permitted user has tab and a link to the tab
    0.25015 seconds ./spec/views/principals/show.html.haml_spec.rb:18

Finished in 4.2 seconds (files took 6.91 seconds to load)
2 examples, 0 failures

Randomized with seed 33681

这是我的测试......

RSpec.describe "/principals/show" do

  before do
    @principal = FactoryGirl.build_stubbed(:principal)
    FactoryGirl.build_stubbed(:financial_statement, principal: @principal)
    @financial_statements = @principal.financial_statements.paginate(per_page: 5, page: 1)
    @merchants_index = MerchantsIndex.new(@principal)
    allow(@view).to receive(:current_user).and_return(mock_model(User).as_null_object)
    allow(@view).to receive(:permitted_to?).and_return(true)
  end

  #it "works" do
  #  expect { render }.not_to raise_error
  #end

  describe "the MINDEX tab" do
    context "a permitted user" do
      it "has tab and a link to the tab" do
        expect(@view).to receive(:permitted_to?).with(:read, :merchants_index).twice.and_return(true)
        render
        expect(rendered).to have_selector("a[href='#mindex']")
        expect(rendered).to have_selector("div#mindex")
      end
    end

    context "a not-permitted user" do
      it "does not have tab or a link to the tab" do
        expect(@view).to receive(:permitted_to?).with(:read, :merchants_index).twice.and_return(false)
        render
        expect(rendered).not_to have_selector("a[href='#mindex']")
        expect(rendered).not_to have_selector("div#mindex")
      end
    end
  end

end

0 个答案:

没有答案