如果我在终端运行rspec spec,我会看到这个错误:
3:in `<top (required)>': undefined local variable or method `user' for main:Object (NameError)
from /home/marketa/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `load'
from /home/marketa/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `block in load'
from /home/marketa/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency'
from /home/marketa/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `load'
from /home/marketa/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1327:in `block in load_spec_files'
from /home/marketa/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `each'
from /home/marketa/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.3.2/lib/rspec/core/configuration.rb:1325:in `load_spec_files'
from /home/marketa/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:102:in `setup'
from /home/marketa/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:88:in `run'
from /home/marketa/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:73:in `run'
from /home/marketa/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.3.2/lib/rspec/core/runner.rb:41:in `invoke'
from /home/marketa/.rvm/gems/ruby-2.2.1/gems/rspec-core-3.3.2/exe/rspec:4:in `<top (required)>'
from /home/marketa/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `load'
from /home/marketa/.rvm/gems/ruby-2.2.1/bin/rspec:23:in `<main>'
from /home/marketa/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
from /home/marketa/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'
我的app / models / user.rb如下所示:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
belongs_to :contact, :foreign_key => "contact_id", dependent: :destroy
accepts_nested_attributes_for :contact
has_many :advertisements
has_many :reviews
#accepts_nested_attributes_for :contact
#accepts_nested_attributes_for :contact>>>>>>> e29ade655eb7ee8f553f049b9fbb505581246758
after_create :welcomer
before_create :set_default_role
validates :email, presence: true
ROLES = %w[admin user]
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
def get_replies_where_advertisement(advertisement, user)
Reply.where("advertisement_id = ? OR receiver = ?", advertisement.id, user.id).order(created_at: :DESC)
end
def get_replies_user(reply)
user = User.find_by(:id => reply.user_id)
end
private
def welcomer
UserMailer.welcome_email(self)
end
def set_default_role
if self.role.blank?
self.role = 'user'
end
end
end
我的spec / models / user_spec.rb如下所示:
require 'rails_helper'
RSpec.describe user, type: :model do
describe "relation" do
it {should has_many :advertisements}
it {should has_many :reviews}
it {should belongs_to(:contact).dependent(:destroy)}
end
describe "validation" do
it {should validate_presence_of :email}
end
end
我的spec / factories / user.rb看起来像这样
require 'faker'
FactoryGirl.define do
factory :user do
end
end
请帮忙,谢谢
答案 0 :(得分:3)
你应该
Rspec.describe User, type: :model do
#code