Testing presence and belongs_to with Rspec

Viewed 8292

I'm trying to learn how to test with Rspec.

At the moment I have a spec for an Item class:

require 'spec_helper'

describe Item do
  it { should belong_to :list }

  before(:each) do
    @item = FactoryGirl.create(:item)
  end

  subject { @item }

  it { should respond_to :name }
  it { should validate_presence_of :name }

end

I have a couple of questions about this , though.

Is it { should validate_presence_of :name } the same as writing:

describe "when name is not present" do
  before { @item.name = "" }
  it { should_not be_valid }
end

or is there a crucial difference between the two?

I was also wondering if it { should belong_to :list } is worth writing in a spec, or if there's a better way for this.

I also know I can do @item = FactoryGirl.build(:item) or FactoryGirl.create(:item). Does create save the item to the test db and build doesn't? Or am I confused here. When should I use which?

Thanks.

1 Answers
Related