使用 Rspec 测试模型中的关系和方法 [英] Testing the relationships and Methods In Model using Rspec
本文介绍了使用 Rspec 测试模型中的关系和方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在从事 rails 项目,我是 rails 新手,我想使用 rspec 测试我的模型关系和模型中的方法.我怎么能这样做我的模型关系是这样的
I m working on rails project and i m new on rails and I want to test my model relationships and methods in model using rspec. How could I do this my model relationships are like this
class Idea < ActiveRecord::Base
belongs_to :person
belongs_to :company
has_many :votes
validates_presence_of :title, :description
def published?
if self.status == "published"
return true
else
return false
end
end
def image_present
if self.image_url
return self.image_url
else
return "/images/image_not_found.jpg"
end
end
def voted
self.votes.present?
end
end
我的idea_spec.rb 文件是
My idea_spec.rb file is
require 'spec_helper'
describe Idea do
it "should have title" do
Idea.new(:title=>'hello',:description=>'some_desc').should be_valid
end
it "should have description" do
Idea.new(:title=>'hello',:description=>'some_desc').should be_valid
end
end
推荐答案
如果你使用 shoulda-matchers (https://github.com/thoughtbot/shoulda-matchers) gem 你可以把这些写成 it{should归属于(:人)}
If you use shoulda-matchers (https://github.com/thoughtbot/shoulda-matchers) gem you can write these as it{should belong_to(:person)}
但老实说,我并没有从这些类型的测试中得到很多,AR 测试得很好.
But to be honest I don't get a lot out of these types of test, AR is well tested.
这篇关于使用 Rspec 测试模型中的关系和方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文