Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'spec_helper'
- describe Contact do
- it "has a valid factory" do
- FactoryGirl.create(:contact).should be_valid
- end
- it "is invalid without a firstname"
- it "is invalid without a lastname"
- it "returns a contact's full name as a string"
- end
- require 'faker'
- FactoryGirl.define do
- factory :contact do |f|
- f.firstname { Faker::Name.first_name }
- f.lastname { Faker::Name.last_name }
- end
- end
- 1) Contact has a valid factory
- Failure/Error: FactoryGirl.create(:contact).should be_valid
- NoMethodError:
- undefined method `firstname=' for #<Contact name: nil, email: nil, content: nil>
- # ./spec/models/contact_spec.rb:5:in `block (2 levels) in <top (required)>'
- Finished in 0.22028 seconds
- 4 examples, 1 failure, 3 pending
- class Contact < ActiveRecord::Base
- has_no_table
- column :name, :string
- column :email, :string
- column :content, :string
- validates_presence_of :name
- validates_presence_of :email
- validates_presence_of :content
- validates_format_of :email, :with => /A[-a-z0-9_+.]+@([-a-z0-9]+.)+[a-z0-9]{2,4}z/i
- validates_length_of :content, :maximum => 500
- def update_spreadsheet
- connection = GoogleDrive.login(ENV["GMAIL_USERNAME"], ENV["GMAIL_PASSWORD"])
- ss = connection.spreadsheet_by_title('Learn-Rails-Example')
- if ss.nil?
- ss = connection.create_spreadsheet('Learn-Rails-Example')
- end
- ws = ss.worksheets[0]
- last_row = 1 + ws.num_rows
- ws[last_row, 1] = Time.new
- ws[last_row, 2] = self.name
- ws[last_row, 3] = self.email
- ws[last_row, 4] = self.content
- ws.save
- end
- end
- class ContactsController < ApplicationController
- def new
- @contact = Contact.new
- end
- def create
- @contact = Contact.new(secure_params[:contact])
- if @contact.valid?
- @contact.update_spreadsheet
- UserMailer.contact_email(@contact).deliver
- flash[:notice] = "Message sent from #{@contact.name}."
- redirect_to root_path
- else
- render :new
- end
- end
- private
- def secure_params
- params.require(:contact).permit(:name, :email, :content)
- end
- end
- 1) Contact has a valid factory
- Failure/Error: FactoryGirl.create(:contact).should be_valid
- ActiveRecord::RecordInvalid:
- Validation failed: Email can't be blank, Email is invalid, Content can't be blank
- # ./spec/models/contact_spec.rb:5:in `block (2 levels) in <top (required)>'
- Finished in 0.22596 seconds
- 4 examples, 1 failure, 3 pending
- Failed examples:
- rspec ./spec/models/contact_spec.rb:4 # Contact has a valid factory
- 1) Contact has a valid factory
- Failure/Error: FactoryGirl.create(:contact).should be_valid
- ActiveRecord::RecordInvalid:
- Validation failed: Content can't be blank
- # ./spec/models/contact_spec.rb:5:in `block (2 levels) in <top (required)>'
- 1) Contact has a valid factory
- Failure/Error: FactoryGirl.create(:contact).should be_valid
- ActiveRecord::Tableless::NoDatabase:
- Can't #create_record a Tableless object
- # ./spec/models/contact_spec.rb:5:in `block (2 levels) in <top (required)>'
- describe Contact do
- it "has a valid factory" do
- FactoryGirl.build(:contact).should be_valid
- end
- it "is invalid without a firstname" do
- FactoryGirl.build(:contact, lastname: nil).should_not be_valid
- end
- it "is invalid without a lastname" do
- FactoryGirl.build(:contact, lastname: nil).should_not be_valid
- end
- it "returns a contact's full name as a string"
- end
- is invalid without a lastname
- has a valid factory
- is invalid without a firstname
- returns a contact's full name as a string (PENDING: Not yet implemented)
- Pending:
- Contact returns a contact's full name as a string
- # Not yet implemented
- # ./spec/models/contact_spec.rb:15
- Finished in 0.22146 seconds
- 4 examples, 0 failures, 1 pending
- FactoryGirl.define do
- factory :contact do |f|
- f.firstname { Faker::Name.first_name }
- f.lastname { Faker::Name.last_name }
- f.email { Faker::Internet.email }
- f.content 'This is content'
- end
- end
- FactoryGirl.build(:contact).should be_valid
- f.email 'email@example.com'
- f.content 'a content'
- validates_presence_of :name
- validates_presence_of :email
- validates_presence_of :content
- validates_format_of :email, :with => /A[-a-z0-9_+.]+@([-a-z0-9]+.)+[a-z0-9]{2,4}z/i
- validates_length_of :content, :maximum => 500
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement