Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. require 'spec_helper'
  2.  
  3. describe Contact do
  4. it "has a valid factory" do
  5. FactoryGirl.create(:contact).should be_valid
  6. end
  7. it "is invalid without a firstname"
  8. it "is invalid without a lastname"
  9. it "returns a contact's full name as a string"
  10. end
  11.  
  12. require 'faker'
  13.  
  14. FactoryGirl.define do
  15. factory :contact do |f|
  16. f.firstname { Faker::Name.first_name }
  17. f.lastname { Faker::Name.last_name }
  18. end
  19. end
  20.  
  21. 1) Contact has a valid factory
  22. Failure/Error: FactoryGirl.create(:contact).should be_valid
  23. NoMethodError:
  24. undefined method `firstname=' for #<Contact name: nil, email: nil, content: nil>
  25. # ./spec/models/contact_spec.rb:5:in `block (2 levels) in <top (required)>'
  26. Finished in 0.22028 seconds
  27. 4 examples, 1 failure, 3 pending
  28.  
  29. class Contact < ActiveRecord::Base
  30. has_no_table
  31.  
  32. column :name, :string
  33. column :email, :string
  34. column :content, :string
  35.  
  36. validates_presence_of :name
  37. validates_presence_of :email
  38. validates_presence_of :content
  39. validates_format_of :email, :with => /A[-a-z0-9_+.]+@([-a-z0-9]+.)+[a-z0-9]{2,4}z/i
  40. validates_length_of :content, :maximum => 500
  41.  
  42. def update_spreadsheet
  43. connection = GoogleDrive.login(ENV["GMAIL_USERNAME"], ENV["GMAIL_PASSWORD"])
  44. ss = connection.spreadsheet_by_title('Learn-Rails-Example')
  45. if ss.nil?
  46. ss = connection.create_spreadsheet('Learn-Rails-Example')
  47. end
  48. ws = ss.worksheets[0]
  49. last_row = 1 + ws.num_rows
  50. ws[last_row, 1] = Time.new
  51. ws[last_row, 2] = self.name
  52. ws[last_row, 3] = self.email
  53. ws[last_row, 4] = self.content
  54. ws.save
  55. end
  56.  
  57. end
  58.  
  59. class ContactsController < ApplicationController
  60.  
  61. def new
  62. @contact = Contact.new
  63. end
  64.  
  65. def create
  66. @contact = Contact.new(secure_params[:contact])
  67. if @contact.valid?
  68. @contact.update_spreadsheet
  69. UserMailer.contact_email(@contact).deliver
  70. flash[:notice] = "Message sent from #{@contact.name}."
  71. redirect_to root_path
  72. else
  73. render :new
  74. end
  75. end
  76.  
  77. private
  78.  
  79. def secure_params
  80. params.require(:contact).permit(:name, :email, :content)
  81. end
  82.  
  83. end
  84.  
  85. 1) Contact has a valid factory
  86. Failure/Error: FactoryGirl.create(:contact).should be_valid
  87. ActiveRecord::RecordInvalid:
  88. Validation failed: Email can't be blank, Email is invalid, Content can't be blank
  89. # ./spec/models/contact_spec.rb:5:in `block (2 levels) in <top (required)>'
  90.  
  91. Finished in 0.22596 seconds
  92. 4 examples, 1 failure, 3 pending
  93.  
  94. Failed examples:
  95.  
  96. rspec ./spec/models/contact_spec.rb:4 # Contact has a valid factory
  97.  
  98. 1) Contact has a valid factory
  99. Failure/Error: FactoryGirl.create(:contact).should be_valid
  100. ActiveRecord::RecordInvalid:
  101. Validation failed: Content can't be blank
  102. # ./spec/models/contact_spec.rb:5:in `block (2 levels) in <top (required)>'
  103.  
  104. 1) Contact has a valid factory
  105. Failure/Error: FactoryGirl.create(:contact).should be_valid
  106. ActiveRecord::Tableless::NoDatabase:
  107. Can't #create_record a Tableless object
  108. # ./spec/models/contact_spec.rb:5:in `block (2 levels) in <top (required)>'
  109.  
  110. describe Contact do
  111. it "has a valid factory" do
  112. FactoryGirl.build(:contact).should be_valid
  113. end
  114. it "is invalid without a firstname" do
  115. FactoryGirl.build(:contact, lastname: nil).should_not be_valid
  116. end
  117.  
  118. it "is invalid without a lastname" do
  119. FactoryGirl.build(:contact, lastname: nil).should_not be_valid
  120. end
  121.  
  122. it "returns a contact's full name as a string"
  123. end
  124.  
  125. is invalid without a lastname
  126. has a valid factory
  127. is invalid without a firstname
  128. returns a contact's full name as a string (PENDING: Not yet implemented)
  129.  
  130. Pending:
  131. Contact returns a contact's full name as a string
  132. # Not yet implemented
  133. # ./spec/models/contact_spec.rb:15
  134.  
  135. Finished in 0.22146 seconds
  136. 4 examples, 0 failures, 1 pending
  137.  
  138. FactoryGirl.define do
  139. factory :contact do |f|
  140. f.firstname { Faker::Name.first_name }
  141. f.lastname { Faker::Name.last_name }
  142. f.email { Faker::Internet.email }
  143. f.content 'This is content'
  144. end
  145. end
  146.  
  147. FactoryGirl.build(:contact).should be_valid
  148.  
  149. f.email 'email@example.com'
  150. f.content 'a content'
  151.  
  152. validates_presence_of :name
  153. validates_presence_of :email
  154. validates_presence_of :content
  155. validates_format_of :email, :with => /A[-a-z0-9_+.]+@([-a-z0-9]+.)+[a-z0-9]{2,4}z/i
  156. validates_length_of :content, :maximum => 500
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement