3

我有以下视图规范:

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'

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

4

2 回答 2

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
于 2013-01-28T07:39:03.470 回答
0

这并不理想,但您可以使用shared_subject

要使用它,您基本上:

  1. 将 'subject' 替换为 'shared_subject' 以在整个上下文中共享第一个结果。
  2. (可选)用 'shared_setup' 替换 'before(:each)' 块
  3. 完毕。主题将在一个单一的上下文中共享(嵌套将再次重新设置)。

所以你的例子看起来像:

describe "carts/show" do
  shared_subject 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
于 2015-11-23T23:41:37.833 回答