I have one model class as listed here:

class Post < ActiveRecord::Base
  has_many :comments

  def tags

and below is the best rspec as per my understanding:

context 'tags' do
  let(:post) { Post.find_by_code('TX') }

  it 'returns sorted alphabetically list of available tags' do
    expect(post.tags).to eq(post.comments.map(&:tag).uniq.sort)

Do we any better way to test it or even it is nor require to test.

  • \$\begingroup\$ You sould create a post, add comments to it, call the tags method and compare to a hardcoded ordered result pre processed based on the input you gave it. Now it's not testing anything, it's will always be true since you have basically the same code on both sides of the expectation. \$\endgroup\$ – arieljuod May 9 at 13:05

Your test code is merely a copy of your model code, so you're not really testing anything.

Rather, your unit test should rely on fixtures, and the expected answer should be hard-coded in the test.

  • \$\begingroup\$ Yes I have fixture of Post \$\endgroup\$ – Dinesh Saini May 6 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.