rspec controller

Ruby code posted
created at 25 Aug 21:39

Edit | Back
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require File.dirname(__FILE__) + '/../spec_helper'

describe ContactsController do
  def mock_contact(params={})
    params = {:deliver => true, :save => true, :valid? => true}.merge(params)
    @mock_contact ||= mock('contact', params)
  end
  
  describe "on GET to new" do
    it "should expose new contact as @contact" do
      Contact.should_receive(:new).and_return(@contact)
      get :new
      assigns(:contact).should == @contact
    end
  end

  describe "on POST to create" do
    it "should populate new contact with form data" do
      Contact.should_receive(:new).with({'these' => 'options'}).and_return(mock_contact)
      post :create, :contact => {'these' => 'options'}
    end

    describe "with valid contact" do
      it "should tell the contact to deliver" do
        Contact.should_receive(:new).and_return(mock_contact(:save => true))
        mock_contact.should_receive(:deliver)
        post :create
      end
      
      it "should notify the user of successful submission" do
        Contact.should_receive(:new).and_return(mock_contact(:save => true))
        post :create
        flash[:notice].should == "Contact Message Sent"
      end
      
      it "should redirect the user to the home page" do
        Contact.should_receive(:new).and_return(mock_contact(:save => true))
        post :create
        response.should redirect_to('/')
      end
    end
    
    describe "with invalid contact" do
      it "should render the new contact form again" do
        Contact.stub!(:new).and_return(mock_contact(:valid? => false))
        post :create
        response.should render_template('contacts/new')
      end
    end
  end
end
1.68 KB in 7 ms with coderay