Question

I'm trying to follow these guidelines to write my specs: Better Specs

In Ruby it is a convention to refer to class methods using .method_name or ::method_name, and use #method_name for instance methods. Does the same convention apply to Rails model attributes?

For instance, should my spec be written like this:

describe ' #exam_code should be unique'?

Was it helpful?

Solution

I use this style guide https://github.com/bbatsov/rails-style-guide#rspec

describe '#some_attr' do
  it 'is required' do
    #your spec
  end
end

OTHER TIPS

I'd suggest that, yes, you should follow the RoR convention for attributes. Think about it from this perspective: an 'attribute' in Rails is simply a convenient way to automatically create accessor methods.

As for the rspec, I prefer the approach that has been described above, placing the attributes within their own describe block. I'd typically approach it like this:

describe Test do
  subject { Test.new(exam_code: exam_code) }
  let(:exam_code) { 'test1' }

  describe '#exam_code' do
    context 'when nil' do
      it { should_not be_valid }
    end
  end
end

I tend to put the method name in the describe clause and the condition on it in the it clause as follows:

describe :exam_code do
  it "should be unique" do
    #spec code here
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top