ruby-on-rails – 使用Rspec测试模型中的关系和方法
发布时间:2020-12-17 02:35:18 所属栏目:百科 来源:网络整理
导读:我正在开发rails项目,我是新的rails,我想使用rspec在模型中测试我的模型关系和方法.我怎么能这样做我的模型关系是这样的 class Idea ActiveRecord::Base belongs_to :person belongs_to :company has_many :votes validates_presence_of :title,:description
我正在开发rails项目,我是新的rails,我想使用rspec在模型中测试我的模型关系和方法.我怎么能这样做我的模型关系是这样的
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文件是 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,你可以写它们{should belongs_to(:person)}
但说实话,我没有从这些类型的测试中得到很多,AR经过了很好的测试. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |