changelog shortlog tags manifest raw

changeset: generate rspec_scaffold product.

changeset 18: b97ed3573af2
parent 17:930559402bbe
child 19:a995025031b3
author: moriq <moriq@moriq.com>
date: Mon Mar 10 10:39:23 2008 +0900 (16 years ago)
files: spec/controllers/products_controller_spec.rb spec/controllers/products_routing_spec.rb spec/fixtures/products.yml spec/helpers/products_helper_spec.rb spec/models/product_spec.rb spec/spec.opts spec/views/products/edit.html.erb_spec.rb spec/views/products/index.html.erb_spec.rb spec/views/products/new.html.erb_spec.rb spec/views/products/show.html.erb_spec.rb
description: generate rspec_scaffold product.
--- a/spec/spec.opts	Mon Mar 10 10:14:24 2008 +0900
+++ b/spec/spec.opts	Mon Mar 10 10:39:23 2008 +0900
@@ -1,6 +1,6 @@
 --colour
 --format
-progress
+specdoc
 --loadby
 mtime
 --reverse
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spec/controllers/products_controller_spec.rb	Mon Mar 10 10:39:23 2008 +0900
@@ -0,0 +1,313 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe ProductsController do
+  describe "handling GET /products" do
+
+    before(:each) do
+      @product = mock_model(Product)
+      Product.stub!(:find).and_return([@product])
+    end
+  
+    def do_get
+      get :index
+    end
+  
+    it "should be successful" do
+      do_get
+      response.should be_success
+    end
+
+    it "should render index template" do
+      do_get
+      response.should render_template('index')
+    end
+  
+    it "should find all products" do
+      Product.should_receive(:find).with(:all).and_return([@product])
+      do_get
+    end
+  
+    it "should assign the found products for the view" do
+      do_get
+      assigns[:products].should == [@product]
+    end
+  end
+
+  describe "handling GET /products.xml" do
+
+    before(:each) do
+      @product = mock_model(Product, :to_xml => "XML")
+      Product.stub!(:find).and_return(@product)
+    end
+  
+    def do_get
+      @request.env["HTTP_ACCEPT"] = "application/xml"
+      get :index
+    end
+  
+    it "should be successful" do
+      do_get
+      response.should be_success
+    end
+
+    it "should find all products" do
+      Product.should_receive(:find).with(:all).and_return([@product])
+      do_get
+    end
+  
+    it "should render the found products as xml" do
+      @product.should_receive(:to_xml).and_return("XML")
+      do_get
+      response.body.should == "XML"
+    end
+  end
+
+  describe "handling GET /products/1" do
+
+    before(:each) do
+      @product = mock_model(Product)
+      Product.stub!(:find).and_return(@product)
+    end
+  
+    def do_get
+      get :show, :id => "1"
+    end
+
+    it "should be successful" do
+      do_get
+      response.should be_success
+    end
+  
+    it "should render show template" do
+      do_get
+      response.should render_template('show')
+    end
+  
+    it "should find the product requested" do
+      Product.should_receive(:find).with("1").and_return(@product)
+      do_get
+    end
+  
+    it "should assign the found product for the view" do
+      do_get
+      assigns[:product].should equal(@product)
+    end
+  end
+
+  describe "handling GET /products/1.xml" do
+
+    before(:each) do
+      @product = mock_model(Product, :to_xml => "XML")
+      Product.stub!(:find).and_return(@product)
+    end
+  
+    def do_get
+      @request.env["HTTP_ACCEPT"] = "application/xml"
+      get :show, :id => "1"
+    end
+
+    it "should be successful" do
+      do_get
+      response.should be_success
+    end
+  
+    it "should find the product requested" do
+      Product.should_receive(:find).with("1").and_return(@product)
+      do_get
+    end
+  
+    it "should render the found product as xml" do
+      @product.should_receive(:to_xml).and_return("XML")
+      do_get
+      response.body.should == "XML"
+    end
+  end
+
+  describe "handling GET /products/new" do
+
+    before(:each) do
+      @product = mock_model(Product)
+      Product.stub!(:new).and_return(@product)
+    end
+  
+    def do_get
+      get :new
+    end
+
+    it "should be successful" do
+      do_get
+      response.should be_success
+    end
+  
+    it "should render new template" do
+      do_get
+      response.should render_template('new')
+    end
+  
+    it "should create an new product" do
+      Product.should_receive(:new).and_return(@product)
+      do_get
+    end
+  
+    it "should not save the new product" do
+      @product.should_not_receive(:save)
+      do_get
+    end
+  
+    it "should assign the new product for the view" do
+      do_get
+      assigns[:product].should equal(@product)
+    end
+  end
+
+  describe "handling GET /products/1/edit" do
+
+    before(:each) do
+      @product = mock_model(Product)
+      Product.stub!(:find).and_return(@product)
+    end
+  
+    def do_get
+      get :edit, :id => "1"
+    end
+
+    it "should be successful" do
+      do_get
+      response.should be_success
+    end
+  
+    it "should render edit template" do
+      do_get
+      response.should render_template('edit')
+    end
+  
+    it "should find the product requested" do
+      Product.should_receive(:find).and_return(@product)
+      do_get
+    end
+  
+    it "should assign the found Product for the view" do
+      do_get
+      assigns[:product].should equal(@product)
+    end
+  end
+
+  describe "handling POST /products" do
+
+    before(:each) do
+      @product = mock_model(Product, :to_param => "1")
+      Product.stub!(:new).and_return(@product)
+    end
+    
+    describe "with successful save" do
+  
+      def do_post
+        @product.should_receive(:save).and_return(true)
+        post :create, :product => {}
+      end
+  
+      it "should create a new product" do
+        Product.should_receive(:new).with({}).and_return(@product)
+        do_post
+      end
+
+      it "should redirect to the new product" do
+        do_post
+        response.should redirect_to(product_url("1"))
+      end
+      
+    end
+    
+    describe "with failed save" do
+
+      def do_post
+        @product.should_receive(:save).and_return(false)
+        post :create, :product => {}
+      end
+  
+      it "should re-render 'new'" do
+        do_post
+        response.should render_template('new')
+      end
+      
+    end
+  end
+
+  describe "handling PUT /products/1" do
+
+    before(:each) do
+      @product = mock_model(Product, :to_param => "1")
+      Product.stub!(:find).and_return(@product)
+    end
+    
+    describe "with successful update" do
+
+      def do_put
+        @product.should_receive(:update_attributes).and_return(true)
+        put :update, :id => "1"
+      end
+
+      it "should find the product requested" do
+        Product.should_receive(:find).with("1").and_return(@product)
+        do_put
+      end
+
+      it "should update the found product" do
+        do_put
+        assigns(:product).should equal(@product)
+      end
+
+      it "should assign the found product for the view" do
+        do_put
+        assigns(:product).should equal(@product)
+      end
+
+      it "should redirect to the product" do
+        do_put
+        response.should redirect_to(product_url("1"))
+      end
+
+    end
+    
+    describe "with failed update" do
+
+      def do_put
+        @product.should_receive(:update_attributes).and_return(false)
+        put :update, :id => "1"
+      end
+
+      it "should re-render 'edit'" do
+        do_put
+        response.should render_template('edit')
+      end
+
+    end
+  end
+
+  describe "handling DELETE /products/1" do
+
+    before(:each) do
+      @product = mock_model(Product, :destroy => true)
+      Product.stub!(:find).and_return(@product)
+    end
+  
+    def do_delete
+      delete :destroy, :id => "1"
+    end
+
+    it "should find the product requested" do
+      Product.should_receive(:find).with("1").and_return(@product)
+      do_delete
+    end
+  
+    it "should call destroy on the found product" do
+      @product.should_receive(:destroy)
+      do_delete
+    end
+  
+    it "should redirect to the products list" do
+      do_delete
+      response.should redirect_to(products_url)
+    end
+  end
+end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spec/controllers/products_routing_spec.rb	Mon Mar 10 10:39:23 2008 +0900
@@ -0,0 +1,61 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe ProductsController do
+  describe "route generation" do
+
+    it "should map { :controller => 'products', :action => 'index' } to /products" do
+      route_for(:controller => "products", :action => "index").should == "/products"
+    end
+  
+    it "should map { :controller => 'products', :action => 'new' } to /products/new" do
+      route_for(:controller => "products", :action => "new").should == "/products/new"
+    end
+  
+    it "should map { :controller => 'products', :action => 'show', :id => 1 } to /products/1" do
+      route_for(:controller => "products", :action => "show", :id => 1).should == "/products/1"
+    end
+  
+    it "should map { :controller => 'products', :action => 'edit', :id => 1 } to /products/1/edit" do
+      route_for(:controller => "products", :action => "edit", :id => 1).should == "/products/1/edit"
+    end
+  
+    it "should map { :controller => 'products', :action => 'update', :id => 1} to /products/1" do
+      route_for(:controller => "products", :action => "update", :id => 1).should == "/products/1"
+    end
+  
+    it "should map { :controller => 'products', :action => 'destroy', :id => 1} to /products/1" do
+      route_for(:controller => "products", :action => "destroy", :id => 1).should == "/products/1"
+    end
+  end
+
+  describe "route recognition" do
+
+    it "should generate params { :controller => 'products', action => 'index' } from GET /products" do
+      params_from(:get, "/products").should == {:controller => "products", :action => "index"}
+    end
+  
+    it "should generate params { :controller => 'products', action => 'new' } from GET /products/new" do
+      params_from(:get, "/products/new").should == {:controller => "products", :action => "new"}
+    end
+  
+    it "should generate params { :controller => 'products', action => 'create' } from POST /products" do
+      params_from(:post, "/products").should == {:controller => "products", :action => "create"}
+    end
+  
+    it "should generate params { :controller => 'products', action => 'show', id => '1' } from GET /products/1" do
+      params_from(:get, "/products/1").should == {:controller => "products", :action => "show", :id => "1"}
+    end
+  
+    it "should generate params { :controller => 'products', action => 'edit', id => '1' } from GET /products/1;edit" do
+      params_from(:get, "/products/1/edit").should == {:controller => "products", :action => "edit", :id => "1"}
+    end
+  
+    it "should generate params { :controller => 'products', action => 'update', id => '1' } from PUT /products/1" do
+      params_from(:put, "/products/1").should == {:controller => "products", :action => "update", :id => "1"}
+    end
+  
+    it "should generate params { :controller => 'products', action => 'destroy', id => '1' } from DELETE /products/1" do
+      params_from(:delete, "/products/1").should == {:controller => "products", :action => "destroy", :id => "1"}
+    end
+  end
+end
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spec/fixtures/products.yml	Mon Mar 10 10:39:23 2008 +0900
@@ -0,0 +1,7 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+# one:
+#   column: value
+#
+# two:
+#   column: value
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spec/helpers/products_helper_spec.rb	Mon Mar 10 10:39:23 2008 +0900
@@ -0,0 +1,11 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe ProductsHelper do
+  
+  #Delete this example and add some real ones or delete this file
+  it "should include the ProductHelper" do
+    included_modules = self.metaclass.send :included_modules
+    included_modules.should include(ProductsHelper)
+  end
+  
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spec/models/product_spec.rb	Mon Mar 10 10:39:23 2008 +0900
@@ -0,0 +1,11 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe Product do
+  before(:each) do
+    @product = Product.new
+  end
+
+  it "should be valid" do
+    @product.should be_valid
+  end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spec/views/products/edit.html.erb_spec.rb	Mon Mar 10 10:39:23 2008 +0900
@@ -0,0 +1,19 @@
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+describe "/products/edit.html.erb" do
+  include ProductsHelper
+  
+  before do
+    @product = mock_model(Product)
+    assigns[:product] = @product
+  end
+
+  it "should render edit form" do
+    render "/products/edit.html.erb"
+    
+    response.should have_tag("form[action=#{product_path(@product)}][method=post]") do
+    end
+  end
+end
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spec/views/products/index.html.erb_spec.rb	Mon Mar 10 10:39:23 2008 +0900
@@ -0,0 +1,17 @@
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+describe "/products/index.html.erb" do
+  include ProductsHelper
+  
+  before(:each) do
+    product_98 = mock_model(Product)
+    product_99 = mock_model(Product)
+
+    assigns[:products] = [product_98, product_99]
+  end
+
+  it "should render list of products" do
+    render "/products/index.html.erb"
+  end
+end
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spec/views/products/new.html.erb_spec.rb	Mon Mar 10 10:39:23 2008 +0900
@@ -0,0 +1,20 @@
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+describe "/products/new.html.erb" do
+  include ProductsHelper
+  
+  before(:each) do
+    @product = mock_model(Product)
+    @product.stub!(:new_record?).and_return(true)
+    assigns[:product] = @product
+  end
+
+  it "should render new form" do
+    render "/products/new.html.erb"
+    
+    response.should have_tag("form[action=?][method=post]", products_path) do
+    end
+  end
+end
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/spec/views/products/show.html.erb_spec.rb	Mon Mar 10 10:39:23 2008 +0900
@@ -0,0 +1,16 @@
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+describe "/products/show.html.erb" do
+  include ProductsHelper
+  
+  before(:each) do
+    @product = mock_model(Product)
+
+    assigns[:product] = @product
+  end
+
+  it "should render attributes in <p>" do
+    render "/products/show.html.erb"
+  end
+end
+