Skip to content

Commit

Permalink
Got webrat working across request and controller specs
Browse files Browse the repository at this point in the history
- added --webrat flag for request controller generator
- deprecated --webrat-matchers flag across generators
- Closes rspec#277.
  • Loading branch information
dchelimsky committed Dec 1, 2010
1 parent 6eb2b25 commit 617d675
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 10 deletions.
4 changes: 4 additions & 0 deletions History.md
Expand Up @@ -7,6 +7,10 @@
* Bug fixes
* Depend on railties, activesupport, and actionpack instead of rails (Piotr
Solnica)
* Got webrat integration working properly across different types of specs

* Deprecations
* --webrat-matchers flag for generators is deprecated. use --webrat instead.

### 2.2.0 / 2010-11-28

Expand Down
12 changes: 11 additions & 1 deletion lib/generators/rspec/integration/integration_generator.rb
Expand Up @@ -3,14 +3,24 @@
module Rspec
module Generators
class IntegrationGenerator < Base
class_option :request_specs, :type => :boolean, :default => true, :desc => "Generate request specs"
class_option :request_specs, :type => :boolean, :default => true, :desc => "Generate request specs"
class_option :webrat, :type => :boolean, :default => false, :desc => "Use webrat methods/matchers"
class_option :webrat_matchers, :type => :boolean, :default => false, :desc => "Use webrat methods/matchers (deprecated - use --webrat)"

def generate_request_spec
return unless options[:request_specs]

template 'request_spec.rb',
File.join('spec/requests', class_path, "#{table_name}_spec.rb")
end

protected

def webrat?
RSpec.deprecate("the --webrat-matchers option", "--webrat") if options[:webrat_matchers]
options[:webrat] || options[:webrat_matchers]
end

end
end
end
6 changes: 6 additions & 0 deletions lib/generators/rspec/integration/templates/request_spec.rb
Expand Up @@ -3,7 +3,13 @@
describe "<%= class_name.pluralize %>" do
describe "GET /<%= table_name %>" do
it "works! (now write some real specs)" do
<% if webrat? -%>
visit <%= table_name %>_path
<% else -%>
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
get <%= table_name %>_path
<% end -%>
response.status.should be(200)
end
end
end
6 changes: 4 additions & 2 deletions lib/generators/rspec/scaffold/scaffold_generator.rb
Expand Up @@ -14,7 +14,8 @@ class ScaffoldGenerator < Base

class_option :controller_specs, :type => :boolean, :default => true, :desc => "Generate controller specs"
class_option :view_specs, :type => :boolean, :default => true, :desc => "Generate view specs"
class_option :webrat_matchers, :type => :boolean, :default => false, :desc => "Use webrat matchers in view specs"
class_option :webrat, :type => :boolean, :default => false, :desc => "Use webrat methods/matchers"
class_option :webrat_matchers, :type => :boolean, :default => false, :desc => "Use webrat methods/matchers (deprecated - use --webrat)"
class_option :helper_specs, :type => :boolean, :default => true, :desc => "Generate helper specs"
class_option :routing_specs, :type => :boolean, :default => true, :desc => "Generate routing specs"

Expand Down Expand Up @@ -51,7 +52,8 @@ def generate_routing_spec
protected

def webrat?
options[:webrat_matchers] || @webrat_matchers_requested
RSpec.deprecate("--webrat-matchers", "--webrat") if options[:webrat_matchers]
options[:webrat] || options[:webrat_matchers]
end

def copy_view(view)
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/rspec/scaffold/templates/edit_spec.rb
Expand Up @@ -20,7 +20,7 @@
<% end -%>
end
<% else -%>
# Run the generator again with the --webrat-matchers flag if you want to use webrat matchers
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => <%= file_name %>_path(@<%= file_name %>), :method => "post" do
<% for attribute in output_attributes -%>
assert_select "<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]"
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/rspec/scaffold/templates/index_spec.rb
Expand Up @@ -22,7 +22,7 @@
<% if webrat? -%>
rendered.should have_selector("tr>td", :content => <%= value_for(attribute) %>.to_s, :count => 2)
<% else -%>
# Run the generator again with the --webrat-matchers flag if you want to use webrat matchers
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "tr>td", :text => <%= value_for(attribute) %>.to_s, :count => 2
<% end -%>
<% end -%>
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/rspec/scaffold/templates/new_spec.rb
Expand Up @@ -19,7 +19,7 @@
<% end -%>
end
<% else -%>
# Run the generator again with the --webrat-matchers flag if you want to use webrat matchers
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => <%= table_name %>_path, :method => "post" do
<% for attribute in output_attributes -%>
assert_select "<%= attribute.input_type -%>#<%= file_name %>_<%= attribute.name %>", :name => "<%= file_name %>[<%= attribute.name %>]"
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/rspec/scaffold/templates/show_spec.rb
Expand Up @@ -18,7 +18,7 @@
<% if webrat? -%>
rendered.should contain(<%= value_for(attribute) %>.to_s)
<% else -%>
# Run the generator again with the --webrat-matchers flag if you want to use webrat matchers
# Run the generator again with the --webrat flag if you want to use webrat matchers
rendered.should match(/<%= eval(value_for(attribute)) %>/)
<% end -%>
<% end -%>
Expand Down
10 changes: 9 additions & 1 deletion lib/rspec/rails/example/controller_example_group.rb
Expand Up @@ -157,12 +157,20 @@ module InstanceMethods
end

included do
subject { controller }

metadata[:type] = :controller

before do
@routes = ::Rails.application.routes
ActionController::Base.allow_forgery_protection = false
end
subject { controller }

webrat do
before do
Webrat.configure {|c| c.mode = :rails}
end
end
end

RSpec.configure &include_self_when_dir_matches('spec','controllers')
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/rails/example/helper_example_group.rb
Expand Up @@ -61,6 +61,7 @@ def _controller_path

included do
metadata[:type] = :helper

before do
controller.controller_path = _controller_path
end
Expand Down
21 changes: 19 additions & 2 deletions lib/rspec/rails/example/request_example_group.rb
Expand Up @@ -21,12 +21,21 @@ module RequestExampleGroup
include ActionDispatch::Assertions
include RSpec::Rails::BrowserSimulators

module InstanceMethods
def app
::Rails.application
end
end

webrat do
include Webrat::Matchers
include Webrat::Methods

def app
::Rails.application
module InstanceMethods

def last_response
@response
end
end
end

Expand All @@ -44,6 +53,14 @@ def app
before do
@router = ::Rails.application.routes
end

webrat do
before do
Webrat.configure do |c|
c.mode = :rack
end
end
end
end

RSpec.configure &include_self_when_dir_matches('spec','requests')
Expand Down

0 comments on commit 617d675

Please sign in to comment.