niessner-rspec_controller_macros 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,116 @@
1
+ Installing
2
+ ----------
3
+ 1) Add:
4
+ ENV["RAILS_ENV"] = "test"
5
+ to the top of /spec/spec_helper.rb
6
+ 2) Add:
7
+ config.gem "niessner-rspec_view_macros", :lib => "rspec_view_macros", :source => "http://gems.github.com"
8
+ to your environment.rb inside the Initializer.run block
9
+
10
+ Usage
11
+ ----------
12
+
13
+ Registering roles
14
+ In the configure block of your spec/spec_helper.rb register roles by doing:
15
+
16
+ Rspec::UserRoles.register(:visitor) {nil}
17
+ Rspec::UserRoles.register(:user) {User.new}
18
+ Rspec::UserRoles.register(:admin) do
19
+ user = User.new
20
+ user.admin = true
21
+ user
22
+ end
23
+
24
+ Let's you write specs similar to:
25
+
26
+ describe Admin::ArticlesController do
27
+ describe "get index" do
28
+ as_a(:visitor) {it_should_redirect_to {new_session_path}}
29
+ as_a(:user) {it_should_render_template.permission_denied}
30
+
31
+ as_an :admin do
32
+ before do
33
+ @articles = [Article.new, Article.new]
34
+ stub(Article).find(:all) {@articles}
35
+ end
36
+
37
+ it_should_render_view Admin::Articles::Index
38
+ it_should_render_layout Layouts::Admin
39
+ it_should_set_rendered_view.articles.to {@articles}
40
+ end
41
+ end
42
+
43
+ describe "post create" do
44
+ as_a(:visitor) {it_should_redirect_to {new_session_path}}
45
+ as_a(:user) {it_should_render_template.permission_denied}
46
+
47
+ as_an :admin do
48
+ before {stub(Article).create!("title" => "Title", "body" => "Body")}
49
+ post :article => {:title => "Title", :body => "Body"}
50
+
51
+ it "should call finishing_editing" do
52
+ response.should have_text("finish_editing(null);")
53
+ end
54
+ end
55
+ end
56
+
57
+ describe "post update" do
58
+ as_a(:visitor) {it_should_redirect_to {new_session_path}}
59
+ as_a(:user) {it_should_render_template.permission_denied}
60
+
61
+ as_an :admin do
62
+ before(:each) do
63
+ article = Article.new
64
+ strong.stub(Article).find("5") {article}
65
+ strong.stub(Article).find(:all) {[]}
66
+ strong.stub(article).update_attributes!("title" => "Title", "body" => "Body")
67
+ end
68
+
69
+ post :id => 5, :article => {:title => "Title", :body => "Body"}
70
+
71
+ it "should call finishing_editing" do
72
+ response.should have_text("finish_editing(null);")
73
+ end
74
+ end
75
+ end
76
+
77
+ describe "post publish" do
78
+ as_a(:visitor) {it_should_redirect_to {new_session_path}}
79
+ as_a(:user) {it_should_render_template.permission_denied}
80
+
81
+ as_an :admin do
82
+ before(:each) do
83
+ @article = Article.new
84
+ strong.stub(@article).publish!
85
+ strong.stub(Article).find("5") {@article}
86
+ strong.stub(Article).find(:all) {[]}
87
+ end
88
+
89
+ post :id => 5
90
+
91
+ it "should publish the article" do
92
+ verify_invocation of_spy(@article).publish!
93
+ end
94
+ end
95
+ end
96
+
97
+ describe "post destroy" do
98
+ as_a(:visitor) {it_should_redirect_to {new_session_path}}
99
+ as_a(:user) {it_should_render_template.permission_denied}
100
+
101
+ as_an :admin do
102
+ before(:each) do
103
+ @article = Article.new
104
+ strong.stub(@article).destroy
105
+ strong.stub(Article).find("5") {@article}
106
+ strong.stub(Article).find(:all) {[]}
107
+ end
108
+
109
+ post :id => 5
110
+
111
+ it "should destroy the article" do
112
+ verify_invocation of_spy(@article).destroy
113
+ end
114
+ end
115
+ end
116
+ end
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/gempackagetask'
4
+ require 'date'
5
+
6
+ spec = Gem::Specification.new do |s|
7
+ s.name = "rspec_controller_macros"
8
+ s.version = "0.0.1"
9
+ s.summary = "Some macros to make it more convenient to spec controllers."
10
+ s.files = FileList['[A-Z]*', 'lib/**/*.rb']
11
+ s.require_path = 'lib'
12
+ s.author = "Michael Niessner"
13
+ s.email = "michael@niessner.us"
14
+ s.platform = Gem::Platform::RUBY
15
+ end
16
+
17
+ desc "Generate a gemspec file"
18
+ task :gemspec do
19
+ File.open("#{spec.name}.gemspec", 'w') do |f|
20
+ f.write spec.to_ruby
21
+ end
22
+ end
@@ -0,0 +1,39 @@
1
+ module Rspec
2
+ module ControllerAsAMacro
3
+ module ExampleMethods
4
+ def login_with_user(user)
5
+ @current_user = user
6
+ stub(controller).current_user {@current_user}
7
+ end
8
+ end
9
+
10
+ module ExampleGroupMethods
11
+ def as_a(*role_symbols, &as_a_block)
12
+ as_role(:a, *role_symbols, &as_a_block)
13
+ end
14
+
15
+ def as_an(*role_symbols, &as_a_block)
16
+ as_role(:an, *role_symbols, &as_a_block)
17
+ end
18
+
19
+ protected
20
+ def as_role(description_symbol, *role_symbols, &as_a_block)
21
+ role_symbols.each do |role_symbol|
22
+ describe "as #{description_symbol} #{role_symbol}" do
23
+ before do
24
+ login_with_user Rspec::UserRoles.user_with_role(role_symbol)
25
+ end
26
+
27
+ instance_eval(&as_a_block)
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ def self.included(object)
34
+ object.send :include, ExampleMethods
35
+ object.extend ExampleGroupMethods
36
+ end
37
+ end
38
+ end
39
+
@@ -0,0 +1,17 @@
1
+ module Rspec
2
+ module ControllerBeARedirectMacro
3
+ module ExampleGroupMethods
4
+ def it_should_be_a_redirect
5
+ it "should be a redirect" do
6
+ do_action
7
+ response.should be_redirect
8
+ end
9
+ end
10
+ end
11
+
12
+ def self.included(object)
13
+ object.extend ExampleGroupMethods
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,16 @@
1
+ module Rspec
2
+ module ControllerBeASuccessMacro
3
+ module ExampleGroupMethods
4
+ def it_should_be_a_success
5
+ it "should be a success" do
6
+ do_action
7
+ response.should be_success
8
+ end
9
+ end
10
+ end
11
+
12
+ def self.included(object)
13
+ object.extend ExampleGroupMethods
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,61 @@
1
+ module Rspec
2
+ module ControllerDoActionMacro
3
+ module ExampleMethods
4
+ def described_action
5
+ self.class.description_parts[1].split(" ")
6
+ end
7
+
8
+ def set_post_parameters_to(hash)
9
+ set_action_parameters(hash)
10
+ end
11
+
12
+ def set_get_parameters_to(hash)
13
+ set_action_parameters(hash)
14
+ end
15
+
16
+ def set_put_parameters_to(hash)
17
+ set_action_parameters(hash)
18
+ end
19
+
20
+ def set_delete_parameters_to(hash)
21
+ set_action_parameters(hash)
22
+ end
23
+
24
+ def set_action_parameters(hash)
25
+ @action_parameters = hash
26
+ end
27
+
28
+ def action_parameters(hash=nil)
29
+ hash || @action_parameters
30
+ end
31
+
32
+ def do_post(hash=nil)
33
+ do_action(hash)
34
+ end
35
+
36
+ def do_get(hash=nil)
37
+ do_action(hash)
38
+ end
39
+
40
+ def do_put(hash=nil)
41
+ do_action(hash)
42
+ end
43
+
44
+ def do_delete(hash=nil)
45
+ do_action(hash)
46
+ end
47
+
48
+ def do_action(hash=nil)
49
+ send_parameters = described_action
50
+ send_parameters = send_parameters + [action_parameters(hash)] if action_parameters(hash).not.nil?
51
+ self.send(*send_parameters)
52
+ end
53
+ end
54
+
55
+ def self.included(receiver)
56
+ receiver.send :include, ExampleMethods
57
+ end
58
+ end
59
+ end
60
+
61
+
@@ -0,0 +1,16 @@
1
+ module Rspec
2
+ module ControllerPostMacro
3
+ module ExampleGroupMethods
4
+ def post(hash)
5
+ before do
6
+ set_post_parameters_to(hash)
7
+ do_action
8
+ end
9
+ end
10
+ end
11
+
12
+ def self.included(object)
13
+ object.extend ExampleGroupMethods
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module Rspec
2
+ module ControllerRedirectToMacro
3
+ module ExampleGroupMethods
4
+ def it_should_redirect_to(&block)
5
+ it "should redirect to " do
6
+ do_action
7
+ path = self.instance_eval(&block)
8
+ new_description = instance_variable_get("@_defined_description") + path.to_s
9
+ instance_variable_set("@_defined_description", new_description)
10
+ response.should redirect_to(path)
11
+ end
12
+ end
13
+ end
14
+
15
+ def self.included(object)
16
+ object.extend ExampleGroupMethods
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,37 @@
1
+ module Rspec
2
+ module ControllerRenderTemplateMacro
3
+ module ExampleGroupMethods
4
+ class RenderTemplateProxy < BlankSlate
5
+ def initialize(example_group)
6
+ @example_group = example_group
7
+ end
8
+
9
+ def permission_denied
10
+ @example_group.it_should_render_template_with_template_string("app/views/shared/permission_denied.html.erb")
11
+ end
12
+ end
13
+
14
+ def it_should_render_template(template_string=nil)
15
+ if template_string.nil?
16
+ RenderTemplateProxy.new(self)
17
+ else
18
+ it_should_render_template_with_template_string(template_string)
19
+ end
20
+ end
21
+
22
+ def it_should_render_template_with_template_string(template_string)
23
+ it "should render template #{template_string}" do
24
+ do_action
25
+ response.should render_template(template_string)
26
+ end
27
+ end
28
+ end
29
+
30
+ def self.included(object)
31
+ object.extend ExampleGroupMethods
32
+ end
33
+ end
34
+ end
35
+
36
+
37
+
@@ -0,0 +1,29 @@
1
+ module Rspec
2
+ class UserRoles
3
+ include Singleton
4
+
5
+ class << self
6
+ def register(role, &block)
7
+ instance.register(role, &block)
8
+ end
9
+
10
+ def user_with_role(role)
11
+ instance.user_with_role(role)
12
+ end
13
+ end
14
+
15
+ def register(role, &block)
16
+ roles[role] = block
17
+ end
18
+
19
+ def user_with_role(role)
20
+ raise "Role not found" if roles[role].nil?
21
+ roles[role].call
22
+ end
23
+
24
+ protected
25
+ def roles
26
+ @roles ||= Hash.new
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,16 @@
1
+ ActionController::Base.send(:include, ViewObjects::ActionControllerEnhancements)
2
+
3
+ if ENV["RAILS_ENV"] == "test"
4
+ require 'spec'
5
+ require 'spec/rails'
6
+
7
+ Spec::Runner.configure do |config|
8
+ config.include(Rspec::ControllerAsAMacro, :type => :controllers)
9
+ config.include(Rspec::ControllerDoActionMacro, :type => :controllers)
10
+ config.include(Rspec::ControllerBeASuccessMacro, :type => :controllers)
11
+ config.include(Rspec::ControllerBeARedirectMacro, :type => :controllers)
12
+ config.include(Rspec::ControllerRenderTemplateMacro, :type => :controllers)
13
+ config.include(Rspec::ControllerRedirectToMacro, :type => :controllers)
14
+ config.include(Rspec::ControllerPostMacro, :type => :controllers)
15
+ end
16
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: niessner-rspec_controller_macros
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Michael Niessner
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-11-10 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: michael@niessner.us
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - Rakefile
26
+ - README
27
+ - lib/rspec_controller_macros.rb
28
+ - lib/rspec/controller_as_a_macro.rb
29
+ - lib/rspec/controller_be_a_redirect_macro.rb
30
+ - lib/rspec/controller_be_a_success_macro.rb
31
+ - lib/rspec/controller_do_action_macro.rb
32
+ - lib/rspec/controller_post_macro.rb
33
+ - lib/rspec/controller_redirect_to_macro.rb
34
+ - lib/rspec/controller_render_template_macro.rb
35
+ - lib/rspec/user_roles.rb
36
+ has_rdoc: false
37
+ homepage:
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: Some macros to make it more convenient to spec controllers.
62
+ test_files: []
63
+