如何验证 rspec 中的非活动记录模型?
How to Validate the non active record models in rspec?
#item class
class Item
include ActiveModel::Model
#validations from activemodel class
validates :url, presence: true
validates :text, presence: true
attr_accessor :url
attr_accessor :text
def initialize (url,text)
@url=url
@text=text
end
end
--------------------------------------------------------
RSpec.describe Item, type: :model do
before :each do
@url="www.fb.com"
@text="to see fb"
@item=Item.new(@url,@text)
end
it { (@item).to validate_presence_of(:url) }
end
它在 rspec 中显示错误 validate_presence_of 没有方法错误 我所有剩余的测试都通过了所有验证都显示没有方法错误
确保您的 Gemfile
中有 shoulda
gem。并检查语法,应该是这样的:
describe Item do
it { is_expected.to validate_presence_of(:url) }
end
#item class
class Item
include ActiveModel::Model
#validations from activemodel class
validates :url, presence: true
validates :text, presence: true
attr_accessor :url
attr_accessor :text
def initialize (url,text)
@url=url
@text=text
end
end
--------------------------------------------------------
RSpec.describe Item, type: :model do
before :each do
@url="www.fb.com"
@text="to see fb"
@item=Item.new(@url,@text)
end
it { (@item).to validate_presence_of(:url) }
end
它在 rspec 中显示错误 validate_presence_of 没有方法错误 我所有剩余的测试都通过了所有验证都显示没有方法错误
确保您的 Gemfile
中有 shoulda
gem。并检查语法,应该是这样的:
describe Item do
it { is_expected.to validate_presence_of(:url) }
end