2
\$\begingroup\$

I have one model class as listed here:

class Post < ActiveRecord::Base
  has_many :comments

  def tags
    comments.map(&:tag).uniq.sort
  end
end

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)
  end
end

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

\$\endgroup\$
1
  • \$\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
    Commented May 9, 2019 at 13:05

1 Answer 1

2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Yes I have fixture of Post \$\endgroup\$ Commented May 6, 2019 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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