Testing Relationships and Methods in a Model Using Rspec

I am working on a rails project and I am m new on rails and I want to test my model relationships and methods in a model using rspec. How can i do this my model relationship is 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

 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 
+4
source share
2 answers

If you use gams-matchers (https://github.com/thoughtbot/shoulda-matchers), you can write them as it{should belong_to(:person)}

But to be honest, I don’t know much about these tests, AR is well tested.

+5
source

The simple answer is no. You also do not test has_many or belongs_to . The place for these specifications / tests is in the Rails codebase, not in your codebase. You Idea spec does not get absolutely nothing that Rails does.

Here are the official tests:

Edit: Please read @DavidChelimsky's comment above.

0
source

Source: https://habr.com/ru/post/1395206/


All Articles