aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/contact_validator_spec.rb
blob: 0f5403967a2f26b008e68eafaa837d42d4c75686 (plain)
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
52
53
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe ContactValidator do

    describe :new do

        let(:valid_params) do
            { :name => "Vinny Vanilli",
              :email => "vinny@localhost",
              :subject => "Why do I have such an ace name?",
              :message => "You really should know!!!\n\nVinny" }
        end

        it 'validates specified attributes' do
            ContactValidator.new(valid_params).should be_valid
        end

        it 'validates name is present' do
            valid_params.except!(:name)
            validator = ContactValidator.new(valid_params)
            expect(validator).to have(1).error_on(:name)
        end

        it 'validates email is present' do
            valid_params.except!(:email)
            validator = ContactValidator.new(valid_params)
            # We have 2 errors on email because of the format validator
            expect(validator).to have(2).errors_on(:email)
        end

        it 'validates email format' do
            valid_params.merge!({:email => 'not-an-email'})
            validator = ContactValidator.new(valid_params)
            expect(validator.errors_on(:email)).to include("Email doesn't look like a valid address")
        end

        it 'validates subject is present' do
            valid_params.except!(:subject)
            validator = ContactValidator.new(valid_params)
            expect(validator).to have(1).error_on(:subject)
        end

        it 'validates message is present' do
            valid_params.except!(:message)
            validator = ContactValidator.new(valid_params)
            expect(validator).to have(1).error_on(:message)
        end

    end

end