如何设置一个rspec视图规范,以便只为其所有规范调用一次渲染?

时间:2013-01-28 07:03:08

标签: ruby-on-rails view rspec

我有以下观点规范:

describe "carts/show" do
  before(:each) do
    book = build_stubbed(:product, title: "book")
    car = build_stubbed(:product, title: "car")
    line_item1 = build_stubbed(:line_item, product: book, quantity: 3)
    line_item2 = build_stubbed(:line_item, product: car, quantity: 1)
    @cart = assign(:cart, build_stubbed(:cart, line_items: [line_item1, line_item2]))
    render
  end

  it "displays a single car" do
    assert_select "li", text: "1 time: car"
  end
  it "displays 3 books" do
    assert_select "li", text: "3 times: book"
  end
  it "has exactly 2 items" do
    assert_select "li", 2
  end

end

此规格按预期工作。但是,由于设置位于before(:each)块中,因此每个块都会调用一次设置和render方法。这很慢。理想情况下,只应为整个describe块调用一次。

我认为将before(:each)更改为before(:all)会解决此问题,但我收到了错误(show.html.erb_spec.rb中的第16行是render调用):

NoMethodError: undefined method `example_group' for nil:NilClass
~/.rvm/gems/ruby-1.9.3-p125/gems/rspec-rails-2.12.0/lib/rspec/rails/example/view_example_group.rb:106:in `_default_file_to_render'
~/.rvm/gems/ruby-1.9.3-p125/gems/rspec-rails-2.12.0/lib/rspec/rails/example/view_example_group.rb:112:in `_default_render_options'
~/.rvm/gems/ruby-1.9.3-p125/gems/rspec-rails-2.12.0/lib/rspec/rails/example/view_example_group.rb:45:in `render'
show.html.erb_spec.rb:16:in `block (2 levels) in <top (required)>'
~/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.12.1/lib/rspec/core/hooks.rb:23:in `instance_eval'
~/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.12.1/lib/rspec/core/hooks.rb:23:in `run'
~/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.12.1/lib/rspec/core/hooks.rb:106:in `run'
~/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.12.1/lib/rspec/core/hooks.rb:424:in `run_hook'
~/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.12.1/lib/rspec/core/example_group.rb:319:in `run_before_all_hooks'
~/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.12.1/lib/rspec/core/example_group.rb:368:in `run'
~/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.12.1/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
~/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.12.1/lib/rspec/core/command_line.rb:28:in `map'
~/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.12.1/lib/rspec/core/command_line.rb:28:in `block in run'
~/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.12.1/lib/rspec/core/reporter.rb:34:in `report'
~/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.12.1/lib/rspec/core/command_line.rb:25:in `run'
~/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.12.1/lib/rspec/core/runner.rb:80:in `run'
~/.rvm/gems/ruby-1.9.3-p125/gems/rspec-core-2.12.1/lib/rspec/core/runner.rb:17:in `block in autorun'

我不知道我还能在这做什么。解决这个问题的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

RSpec-Rails'渲染肯定假设它发生在每个测试中,这将使这有点尴尬,我们需要从它的外观中解决内部状态。

从探索rspec-rails开始,看起来它将render调用委托给ActionView::TestCase::Behavior#render@rendered似乎是重要的状态(可能是@view),但我不知道这是否会引入更多陷阱。

这是我完全未经测试的方法:

describe "carts/show" do

  # Memoize the view across test runs for speed; this is some dangerous
  # spelunking in internal state, though!
  rendered_view    = nil
  rendered_content = nil

  before(:each) do
    if rendered_view && rendered_content
      @view     = rendered_view
      @rendered = rendered_content
    else
      book = build_stubbed(:product, title: "book")
      car = build_stubbed(:product, title: "car")
      line_item1 = build_stubbed(:line_item, product: book, quantity: 3)
      line_item2 = build_stubbed(:line_item, product: car, quantity: 1)
      @cart = assign(:cart, build_stubbed(:cart, line_items: [line_item1, line_item2]))

      render

      rendered_view    = @view
      rendered_content = @rendered
    end
  end

  it "displays a single car" do
    assert_select "li", text: "1 time: car"
  end
  it "displays 3 books" do
    assert_select "li", text: "3 times: book"
  end
  it "has exactly 2 items" do
    assert_select "li", 2
  end

end