Reputation: 952
I am following this book and working on the order model validation.
This is my order_spec.rb
:
require 'spec_helper'
RSpec.describe Order, type: :model do
let(:order) { FactoryGirl.build :order }
subject { :order }
it { should respond_to(:total) }
it { should respond_to(:user_id) }
it { should validate_presence_of :user_id }
it { should validate_presence_of :total}
it { should validate_numericality_of(:total).is_greater_than_or_equal_to(0) }
it { should belong_to :user }
it { should have_many(:placements) }
it { should have_many(:products).through(:placements) }
end
My factory:
FactoryGirl.define do
factory :order do
user
total "9.99"
end
end
order.rb
class Order < ApplicationRecord
belongs_to :user
validates :total, presence: true,
numericality: { greater_than_or_equal_to: 0 }
validates :user_id, presence: true
has_many :placements
has_many :products, through: :placements
end
placement.rb
:
class Placement < ApplicationRecord
belongs_to :order, inverse_of: :placements
belongs_to :product, inverse_of: :placements
end
Users model user.rb
class User < ApplicationRecord
validates :auth_token, uniqueness: true
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable,
:rememberable, :trackable, :validatable
before_create :generate_authentication_token!
has_many :products, dependent: :destroy
has_many :orders, dependent: :destroy
def generate_authentication_token!
begin
self.auth_token = Devise.friendly_token
end while self.class.exists?(auth_token: auth_token)
end
# validate :password_must_match
# def password_must_match
# errors.add(:password, "doesn't match confirmation") if password != password_confirmation
# end
end
Now I got this error:
1) Order should have many placements
Failure/Error: it { should have_many(:placements) }
NoMethodError:
undefined method `reflect_on_association' for Symbol:Class
# ./spec/models/order_spec.rb:17:in `block (2 levels) in <top (required)>'
2) Order should require total to be set
Failure/Error: it { should validate_presence_of :total}
NoMethodError:
undefined method `total=' for :order:Symbol
# ./spec/models/order_spec.rb:11:in `block (2 levels) in <top (required)>'
3) Order should respond to #total
Failure/Error: it { should respond_to(:total) }
expected :order to respond to :total
# ./spec/models/order_spec.rb:7:in `block (2 levels) in <top (required)>'
4) Order should respond to #user_id
Failure/Error: it { should respond_to(:user_id) }
expected :order to respond to :user_id
# ./spec/models/order_spec.rb:8:in `block (2 levels) in <top (required)>'
5) Order should only allow numbers for total which are greater than or equal to 0
Failure/Error: it { should validate_numericality_of(:total).is_greater_than_or_equal_to(0) }
NoMethodError:
undefined method `total=' for :order:Symbol
# ./spec/models/order_spec.rb:12:in `block (2 levels) in <top (required)>'
6) Order should require user_id to be set
Failure/Error: it { should validate_presence_of :user_id }
NoMethodError:
undefined method `user_id=' for :order:Symbol
# ./spec/models/order_spec.rb:10:in `block (2 levels) in <top (required)>'
7) Order should belong to user
Failure/Error: it { should belong_to :user }
NoMethodError:
undefined method `reflect_on_association' for Symbol:Class
# ./spec/models/order_spec.rb:14:in `block (2 levels) in <top (required)>'
8) Order should have many products through placements
Failure/Error: it { should have_many(:products).through(:placements) }
NoMethodError:
undefined method `reflect_on_association' for Symbol:Class
# ./spec/models/order_spec.rb:18:in `block (2 levels) in <top (required)>'
I have looked into this question (possible duplicate), but it does not solve my problem.
Upvotes: 0
Views: 731
Reputation: 2586
Your subject
is a symbol subject { :order }
. I think you did mean subject { order }
Upvotes: 2