[英]Testing the relationships and Methods In Model using Rspec
I m working on rails project and im new on rails and I want to test my model relationships and methods in model using rspec. 我正在研究rails项目和我在rails上的新功能,我想使用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
My idea_spec.rb file is 我的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
If you use shoulda-matchers (https://github.com/thoughtbot/shoulda-matchers) gem you can write these as it{should belong_to(:person)}
如果你使用shoulda-matchers(https://github.com/thoughtbot/shoulda-matchers)gem,你可以写它们
it{should belong_to(:person)}
But to be honest I don't get a lot out of these types of test, AR is well tested. 但说实话,我没有从这些类型的测试中得到很多,AR经过了很好的测试。
Simple answer is, you do not. 简单的答案是,你没有。 Neither do you test
has_many
or belongs_to
. 你也没有测试
has_many
或belongs_to
。 The place for these specs / tests is in Rails codebase, and not in your codebase. 这些规范/测试的位置在Rails代码库中,而不在您的代码库中。 You
Idea
spec gives you absolutely nothing that Rails does not. 你的
Idea
规范绝对没有Rails没有。
Here are the official tests: 以下是官方测试:
Edit: Please read @DavidChelimsky's comment above. 编辑:请阅读@ DavidChelimsky上面的评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.