Permalink
Browse files

Converted the Rails 2 generator specs into cucumber scenarios

  • Loading branch information...
1 parent 9963445 commit 149ebb63acd7763ebd2ca26d60d18308558dcaa9 @jferris committed Aug 20, 2010
@@ -0,0 +1,67 @@
+Feature: Generate views in a Rails 2.3 application
+
+ Background:
+ When I generate a new rails 2 application
+ And I configure the rails 2 preinitializer to use bundler
+ And I save the following as "Gemfile"
+ """
+ source "http://rubygems.org"
+ gem 'rails', '2.3.8'
+ gem 'sqlite3-ruby', :require => 'sqlite3'
+ gem 'effigy', :path => '../../', :require => 'effigy/rails'
+ """
+ When I run "bundle lock"
+
+ Scenario: generate a controller view and template
+ When I run "./script/generate effigy_view users create"
+ Then the following should be saved as "app/views/users/create.html.effigy"
+ """
+ class UsersCreateView < Effigy::Rails::View
+ private
+ def transform
+ # Apply transformations to the template file here:
+ # text('h1', 'Hello')
+ # Assigns from the action are available:
+ # text('h1', @post.title)
+ # Transformations will be applied to to the template file:
+ # app/templates/users/create.html
+ # See the documentation for more information.
+ end
+ end
+ """
+ Then the following should be saved as "app/templates/users/create.html"
+ """
+ <h1>UsersCreateView</h1>
+ <p>Edit me at app/templates/users/create.html</p>
+ <p>Edit my view at app/views/users/create.html.effigy</p>
+ """
+
+ Scenario: generate a layout view and template
+ When I run "./script/generate effigy_view layouts narrow"
+ Then the following should be saved as "app/views/layouts/narrow.html.effigy"
+ """
+ class NarrowLayout < Effigy::Rails::View
+ private
+ def transform
+ # Apply transformations to the template file here:
+ # text('h1', 'Hello')
+ # Assigns from the action are available:
+ # text('h1', @post.title)
+ # Transformations will be applied to to the template file:
+ # app/templates/layouts/narrow.html
+ # See the documentation for more information.
+ html('body', content_for(:layout))
+ end
+ end
+ """
+ Then the following should be saved as "app/templates/layouts/narrow.html"
+ """
+ <html>
+ <body>
+ <h1>NarrowLayout</h1>
+ <p>Edit me at app/templates/layouts/narrow.html</p>
+ <p>Edit my view at app/views/layouts/narrow.html.effigy</p>
+ </body>
+ </html>
+ """
+
File renamed without changes.
@@ -41,3 +41,8 @@
@terminal.run("ruby script/request #{path}")
end
+Then /^the following should be saved as "([^"]*)"$/ do |path, string|
+ contents = IO.read(File.join(RAILS_ROOT, path))
+ contents.strip.should == string.strip
+end
+
@@ -3,6 +3,8 @@
File.join(RAILS_ROOT, 'config', 'preinitializer.rb'))
FileUtils.cp(File.join(TEMPLATE_ROOT, 'boot.rb.template'),
File.join(RAILS_ROOT, 'config', 'boot.rb'))
+ FileUtils.ln_s(File.join(PROJECT_ROOT, 'generators'),
+ File.join(RAILS_ROOT, 'lib', 'generators'))
end
When /^I generate a new rails 2 application$/ do
@@ -1,129 +0,0 @@
-require 'spec_helper'
-
-module TemplateMatchers
- def contain(expected_text)
- simple_matcher("contain the following lines:\n#{expected_text}") do |path, matcher|
- if File.exist?(path)
- actual_text = IO.read(path)
- if actual_text.include?(expected_text)
- true
- else
- matcher.failure_message =
- "Expected to get the following text:\n#{expected_text}\nBut got:\n#{actual_text}"
- false
- end
- else
- matcher.failure_message = "File does not exist"
- false
- end
- end
- end
-
- def rails_command(command)
- FileUtils.cd RAILS_ROOT do
- output = `#{command} 2>&1`
- unless $? == 0
- violated "Command failed: #{command}\n#{output}"
- end
- end
- end
-end
-
-describe "script/generate effigy_view users create" do
-
- include TemplateMatchers
-
- before do
- @controller_name = 'users'
- @view_name = 'create'
- @view_class_name = 'UsersCreateView'
- rails_command "script/generate effigy_view --backtrace #{@controller_name} #{@view_name} 2>&1"
- end
-
- after do
- FileUtils.rm_f(view_path)
- FileUtils.rm_f(template_path)
- end
-
- it "should create a view file" do
- view_path.should contain("class #{@view_class_name} < Effigy::Rails::View")
- view_path.should contain("private")
- view_path.should contain("def transform")
- view_path.should contain(relative_template_path)
- view_path.should contain("end\nend")
- end
-
- it "should create a template file" do
- template_path.should contain("<h1>#{@view_class_name}</h1>")
- template_path.should contain("<p>Edit me at #{relative_template_path}</p>")
- template_path.should contain("<p>Edit my view at #{relative_view_path}</p>")
- end
-
- def view_path
- File.join(RAILS_ROOT, relative_view_path)
- end
-
- def relative_view_path
- File.join('app', 'views', @controller_name, "#{@view_name}.html.effigy")
- end
-
- def template_path
- File.join(RAILS_ROOT, relative_template_path)
- end
-
- def relative_template_path
- File.join('app', 'templates', @controller_name, "#{@view_name}.html")
- end
-
-end
-
-describe "script/generate effigy_view layouts narrow" do
-
- include TemplateMatchers
-
- before do
- @layout_name = 'narrow'
- @layout_class_name = 'NarrowLayout'
- rails_command "script/generate effigy_view --backtrace layouts #{@layout_name} 2>&1"
- end
-
- after do
- FileUtils.rm_f(view_path)
- FileUtils.rm_f(template_path)
- end
-
- it "should create a view file" do
- view_path.should contain("class #{@layout_class_name} < Effigy::Rails::View")
- view_path.should contain("private")
- view_path.should contain("def transform")
- view_path.should contain("html('body', content_for(:layout))")
- view_path.should contain(relative_template_path)
- view_path.should contain("end\nend")
- end
-
- it "should create a template file" do
- template_path.should contain("<html>")
- template_path.should contain("<body>")
- template_path.should contain("<p>Edit me at #{relative_template_path}</p>")
- template_path.should contain("<p>Edit my view at #{relative_view_path}</p>")
- template_path.should contain("</body>")
- template_path.should contain("</html>")
- end
-
- def view_path
- File.join(RAILS_ROOT, relative_view_path)
- end
-
- def relative_view_path
- File.join('app', 'views', 'layouts', "#{@layout_name}.html.effigy")
- end
-
- def template_path
- File.join(RAILS_ROOT, relative_template_path)
- end
-
- def relative_template_path
- File.join('app', 'templates', 'layouts', "#{@layout_name}.html")
- end
-
-end

0 comments on commit 149ebb6

Please sign in to comment.