Skip to content

Commit

Permalink
Helper for rspec functional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jonleighton committed Feb 20, 2012
1 parent eaa2502 commit 1c6a414
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 3 deletions.
32 changes: 32 additions & 0 deletions lib/focused_controller/rspec_functional_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require 'action_controller'
require 'action_view'
require 'action_dispatch'
require 'rspec/rails'
require 'focused_controller/functional_test_helper'

module FocusedController
module RSpecFunctionalHelper
def self.append_features(base)
base.class_eval do
# This must be included first
include RSpec::Rails::ControllerExampleGroup
extend ClassMethods
include FocusedController::FunctionalTestHelper
end

super
end

module ClassMethods
def controller_class
controller = metadata[:example_group][:description_args].first

if controller.respond_to?(:new)
controller
else
super
end
end
end
end
end
4 changes: 4 additions & 0 deletions test/acceptance/app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ def run_command(command)
run_command "ruby -Itest test/unit/controllers/posts_controller_test.rb"
end

it 'runs a functional spec' do
run_command "rspec spec/controllers/posts_controller_spec.rb"
end

it 'runs a unit spec' do
run_command "rspec spec/unit/controllers/posts_controller_spec.rb"
end
Expand Down
59 changes: 59 additions & 0 deletions test/app/spec/controllers/posts_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'spec_helper'

describe PostsController do
include FocusedController::RSpecFunctionalHelper

before do
@post = Post.create(:title => 'Hello', :body => 'Omg')
end

describe PostsController::Index do
it "should get index" do
get
response.should be_success
subject.posts.should_not be_nil
end
end

describe PostsController::New do
it "should get new" do
get
response.should be_success
end
end

describe PostsController::Create do
it "should create post" do
expect { post post: @post.attributes }.to change(Post, :count).by(1)
response.should redirect_to(post_path(subject.post))
end
end

describe PostsController::Show do
it "should show post" do
get id: @post
response.should be_success
end
end

describe PostsController::Edit do
it "should get edit" do
get id: @post
response.should be_success
end
end

describe PostsController::Update do
it "should update post" do
put id: @post, post: @post.attributes
response.should redirect_to(post_path(subject.post))
end
end

describe PostsController::Destroy do
it "should destroy post" do
expect { delete id: @post }.to change(Post, :count).by(-1)
response.should redirect_to(posts_path)
end
end
end
1 change: 1 addition & 0 deletions test/app/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
require File.expand_path("../../config/environment", __FILE__)
require 'rspec'
require 'focused_controller/rspec_helper'
require 'focused_controller/rspec_functional_helper'
19 changes: 19 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
require 'minitest/spec'
require 'minitest/autorun'
require 'focused_controller'
require 'pathname'
require 'ostruct'

require 'rspec/core'

Expand All @@ -10,4 +12,21 @@ module RSpec::Core::DSL
remove_method :describe
end

# Annoying monkey-patches. "require 'rspec/rails'" pulls in 'capybara/rails', if it
# can, and capybara/rails assumes there is a full rails env present. So this is a
# hack to make it not fail.
module Rails
def self.version
'3.0'
end

def self.root
Pathname.new('')
end

def self.application
OpenStruct.new(:env_config => {})
end
end

TEST_ROOT = File.expand_path('..', __FILE__)
3 changes: 0 additions & 3 deletions test/unit/functional_test_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

module FocusedController
module FunctionalTestHelper
class FakeTestCase
end

class FakePostsController
class Index; end
class Show; end
Expand Down
38 changes: 38 additions & 0 deletions test/unit/rspec_functional_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'helper'
require 'focused_controller/rspec_functional_helper'

module FocusedController
module RSpecFunctionalHelper
class FakePostsController
class Action < ActionController::Base; end
class Index < Action; end
class Show < Action; end
end

index_spec = RSpec::Core::ExampleGroup.describe FakePostsController::Index do
include RSpec::Rails::ControllerExampleGroup
include FocusedController::RSpecFunctionalHelper
end

show_spec = nil
RSpec::Core::ExampleGroup.describe FakePostsController do
include RSpec::Rails::ControllerExampleGroup
include FocusedController::RSpecFunctionalHelper

show_spec = describe(FakePostsController::Show) {}
end

describe RSpecFunctionalHelper do
subject { index_spec.new }

it 'automatically determines the controller class' do
index_spec.controller_class.must_equal FakePostsController::Index
show_spec.controller_class.must_equal FakePostsController::Show
end

it 'includes the FocusedController::FunctionalTestHelper' do
subject.is_a?(FocusedController::FunctionalTestHelper).must_equal true
end
end
end
end

0 comments on commit 1c6a414

Please sign in to comment.