Skip to content

Instantly share code, notes, and snippets.

@davidmiani
Created January 28, 2013 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidmiani/4653495 to your computer and use it in GitHub Desktop.
Save davidmiani/4653495 to your computer and use it in GitHub Desktop.
Problem with using before(:all) in a view spec
require 'spec_helper'
describe "carts/show" do
# using before(:all) rather than before(:each) causes the error:
# 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:10:in `block (2 levels) in <top (required)>'
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment