Got some controller specs working
authorNeil Smith <neil.git@njae.me.uk>
Mon, 6 Jan 2014 14:28:01 +0000 (14:28 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Mon, 6 Jan 2014 14:28:01 +0000 (14:28 +0000)
app/controllers/feed_controller.rb
spec/controllers/feed_controller_spec.rb
spec/factories.rb [new file with mode: 0644]
spec/helpers/feed_helper_spec.rb [deleted file]

index 8e89663852aa502a7da8eead64a9726e22dc928c..0339706833e51ea93d659faabaa5a7fe04d74a85 100644 (file)
@@ -19,7 +19,7 @@ class FeedController < ApplicationController
       respond_to do |format|
         if @feed_items == []
           flash[:notice] = "No items in feed #{@feed_name}"
-          format.html { redirect_to index_url }
+          format.html { redirect_to index_path }
           format.rss  { render :layout => false }
         else
           format.html
@@ -29,7 +29,7 @@ class FeedController < ApplicationController
     else
       respond_to do |format|
         flash[:notice] = "Invalid feed name"
-        format.html { redirect_to index_url }
+        format.html { redirect_to index_path }
         format.rss  { head :not_found}
       end
     end
@@ -51,7 +51,7 @@ class FeedController < ApplicationController
     else
       respond_to do |format|
         flash[:notice] = "Invalid feed name"
-        format.html { redirect_to index_url }
+        format.html { redirect_to index_path }
         format.rss  { head :not_found }
       end
     end
@@ -68,13 +68,13 @@ class FeedController < ApplicationController
     item.save!
     flash[:notice] = "Element #{params[:title]} created"
     respond_to do |format|
-      format.html { redirect_to feed_url(params[:feed_name]) }
+      format.html { redirect_to feed_path(params[:feed_name]) }
       format.rss  { head :ok }
     end
   rescue ActiveRecord::RecordInvalid => error
     flash[:notice] = "Element #{params[:title]} could not be created"
     respond_to do |format|
-      format.html { redirect_to feed_url(params[:feed_name]) }
+      format.html { redirect_to feed_path(params[:feed_name]) }
       format.rss  { head :unprocessable_entity }
     end
   end
@@ -84,13 +84,13 @@ class FeedController < ApplicationController
     if item.update_attribute(:description, params[:description])
       flash[:notice] = "Element #{params[:title]} updated"
       respond_to do |format|
-        format.html { redirect_to feed_url(params[:feed_name]) }
+        format.html { redirect_to feed_path(params[:feed_name]) }
         format.rss  { head :ok }
       end
     else
       flash[:notice] = "Element #{params[:title]} could not be updated"
       respond_to do |format|
-        format.html { redirect_to feed_url(params[:feed_name]) }
+        format.html { redirect_to feed_path(params[:feed_name]) }
         format.rss  { head :unprocessable_entity }
       end
     end
@@ -101,13 +101,13 @@ class FeedController < ApplicationController
     if item.destroy
       flash[:notice] = "Element #{params[:title]} deleted"
       respond_to do |format|
-        format.html { redirect_to feed_url(params[:feed_name]) }
+        format.html { redirect_to feed_path(params[:feed_name]) }
         format.rss  { head :ok }
       end
     else
       flash[:notice] = "Element #{params[:title]} could not be deleted"
       respond_to do |format|
-        format.html { redirect_to feed_url(params[:feed_name]) }
+        format.html { redirect_to feed_path(params[:feed_name]) }
         format.rss  { head :unprocessable_entity }
       end
     end
index 7d40b1a82fee02f8cb9e73017e664eafc5dcede0..fd477916a9dfd299536f8518d6758afbcaf76c5a 100644 (file)
@@ -1,5 +1,85 @@
 require 'spec_helper'
 
 describe FeedController do
+#   describe "POST create" do
+#     let (:feed_item1) { mock_model(FeedItem).as_null_object }
+#     
+#     before do
+#       FeedItem.stub(:new).and_return(feed_item)
+#     end
+#   end
+    
+  describe "GET #index" do
+    let!(:feed_item1) { FactoryGirl.create(:feed_item, feed_name: "feed1") }
+    let!(:feed_item2) { FactoryGirl.create(:feed_item, feed_name: "feed2") }
 
+    it "responds successfully with an HTTP 200 status code" do
+      get :index
+      expect(response).to be_success
+      expect(response.status).to eq(200)
+    end
+
+    it "renders the index template" do
+      get :index
+      expect(response).to render_template("index")
+    end
+
+    it "loads all the feed names into @feeds" do
+      get :index
+      expect(assigns(:feeds).map {|f| f.feed_name}).to match_array(["feed1", "feed2"])
+    end
+  end
+
+  describe "GET #feed" do
+    let!(:feed_item1) { FactoryGirl.create(:feed_item, feed_name: "test_feed", title: "item 1") }
+    let!(:feed_item2) { FactoryGirl.create(:feed_item, feed_name: "test_feed", title: "item 2") }
+    
+    it "redirects an emtpy feed to the index" do
+      get :show, feed_name: "empty_feed"
+      expect(response).to redirect_to(index_path)
+    end 
+    
+    it "responds successfully with an HTTP 200 status code" do
+      get :show, feed_name: "test_feed"
+      expect(response).to be_success
+      expect(response.status).to eq(200)
+    end
+
+    it "renders the index template" do
+      get :show, feed_name: "test_feed"
+      expect(response).to render_template("show")
+    end
+
+    it "loads all of the items of a feed into @feed_items" do
+      get :show, feed_name: "test_feed"
+      expect(assigns(:feed_items)).to match_array([feed_item1, feed_item2])
+    end
+  end
+  
+  
 end
+
+
+# describe MessagesController do
+# describe "POST create" do
+# let(:message) { mock_model(Message).as_null_object }
+# before do
+# Message.stub(:new).and_return(message)
+# end
+# it "creates a new message" do
+# Message.should_receive(:new).
+# with("text" => "a quick brown fox").
+# and_return(message)
+# post :create, :message => { "text" => "a quick brown fox" }
+# end
+# it "saves the message" do
+# message.should_receive(:save)
+# post :create
+# end
+# it "redirects to the Messages index" do
+# post :create
+# response.should redirect_to(:action => "index")
+# end
+# end
+# end
+# 
diff --git a/spec/factories.rb b/spec/factories.rb
new file mode 100644 (file)
index 0000000..a94df2c
--- /dev/null
@@ -0,0 +1,8 @@
+# This will guess the User class
+FactoryGirl.define do
+  factory :feed_item do
+    feed_name "test_feed"
+    title "Test feed item"
+    description "Test feed description"
+  end
+end
\ No newline at end of file
diff --git a/spec/helpers/feed_helper_spec.rb b/spec/helpers/feed_helper_spec.rb
deleted file mode 100644 (file)
index 614ffda..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-require 'spec_helper'
-
-# Specs in this file have access to a helper object that includes
-# the FeedHelper. For example:
-#
-# describe FeedHelper do
-#   describe "string concat" do
-#     it "concats two strings with spaces" do
-#       expect(helper.concat_strings("this","that")).to eq("this that")
-#     end
-#   end
-# end
-describe FeedHelper do
-  pending "add some examples to (or delete) #{__FILE__}"
-end