Permalink
Browse files

added more steps to test

  • Loading branch information...
1 parent 4c4432a commit 1ef67459fd2a7e2b603f6e380353100582c6f909 @mattetti committed Oct 19, 2008
Showing with 83 additions and 11 deletions.
  1. +3 −0 .gitignore
  2. +80 −11 thor-stuff/test-app.thor
View
@@ -0,0 +1,3 @@
+.DS_Store
+*/**/.DS_Store
+thor-stuff/test-app
View
@@ -12,15 +12,18 @@ class TestApp < Thor
edit_request_specs
edit_index_view
make_specs_not_pending
- Dir.chdir(Dir.pwd + "/" + @app_name) do
- print "cd #{@app_name} && rake spec\n"
- print `rake spec`
- end
- # puts 'then `http://localhost:4000/articles`'
+ add_model_validation
+ add_model_specs
+ edit_layout
+ run_app_specs
end
protected
+ def app_path
+ "#{Dir.pwd}/#{@app_name}"
+ end
+
def gsub_file(destination, regexp, *args, &block)
path = destination
content = File.read(path).gsub(regexp, *args, &block)
@@ -37,29 +40,66 @@ class TestApp < Thor
end
def generate_article_resource
- Dir.chdir(Dir.pwd + "/" + @app_name) do
+ Dir.chdir(app_path) do
`merb-gen resource article title:String,author:String,created_at:DateTime`
end
end
def migrate_db
- Dir.chdir(Dir.pwd + "/" + @app_name) do
+ Dir.chdir(app_path) do
`rake db:automigrate MERB_ENV=test`
end
end
+ def edit_layout
+ print " > editing the layout\n"
+
+ sentinel = "<%#= message[:notice] %>"
+ layout_file = "#{app_path}/app/views/layout/application.html.erb"
+ gsub_file layout_file, /(#{Regexp.escape(sentinel)})/mi do |match|
+ <<-RUBY
+<%= message[:notice] %>
+ <%= message[:error] %>
+RUBY
+ end
+
+ end
+
def edit_request_specs
print " > editing the request specs\n"
- sentinel = ":params => { :article => { }})"
- spec_file = "#{Dir.pwd}/#{@app_name}/spec/requests/articles_spec.rb"
+ sentinel = ":params => { :article => { :id => nil }})"
+ spec_file = "#{app_path}/spec/requests/articles_spec.rb"
gsub_file spec_file, /(#{Regexp.escape(sentinel)})/mi do |match|
":params => { :article => {:title => 'intro', :author => 'Matt', :created_at => '#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}' }})"
end
+
+ ### failing_post_spec
+ failing_post_spec = <<-RUBY
+ \n
+ describe "a failing POST" do
+ before(:each) do
+ @response = request(resource(:articles), :method => "POST", :params => { :article => { :id => nil}})
+ end
+
+ it "should re render the new action" do
+ @response.body.include?("Articles controller, new action").should be_true
+ end
+
+ it "should have an error message" do
+ @response.body.include?("Article failed to be created").should be_true
+ end
+ end
+RUBY
+
+ gsub_file spec_file, /(end\n\n)$/i do |match|
+ "#{failing_post_spec}\n#{match}"
+ end
+
end
def edit_index_view
- spec_file = "#{Dir.pwd}/#{@app_name}/app/views/articles/index.html.erb"
+ spec_file = "#{app_path}/app/views/articles/index.html.erb"
print " > editing index view #{spec_file}\n"
index_view = <<-RUBY
<ul>
@@ -76,10 +116,39 @@ RUBY
def make_specs_not_pending
print " > removing the spec pending status\n"
sentinel = "pending"
- spec_file = "#{Dir.pwd}/#{@app_name}/spec/requests/articles_spec.rb"
+ spec_file = "#{app_path}/spec/requests/articles_spec.rb"
gsub_file spec_file, /(#{Regexp.escape(sentinel)})/mi do |match|
""
end
end
+
+ def add_model_validation
+ print " > adding title validation to Article\n"
+ model_file = "#{app_path}/app/models/article.rb"
+ gsub_file model_file, /(end)\s*$/mi do |match|
+ " validates_present :title\n#{match}"
+ end
+ end
+
+ def add_model_specs
+ print " > adding model specs\n"
+ model_spec = "#{app_path}/spec/models/article_spec.rb"
+ validation_spec = <<-RUBY
+it "should not be valid without a title" do
+ @article = Article.new
+ @article.should_not be_valid
+ end
+ RUBY
+ gsub_file model_spec, /(it "should have specs")/mi do |match|
+ validation_spec
+ end
+ end
+
+ def run_app_specs
+ Dir.chdir(Dir.pwd + "/" + @app_name) do
+ print "cd #{@app_name} && rake spec\n"
+ print `rake spec`
+ end
+ end
end

0 comments on commit 1ef6745

Please sign in to comment.