2011-07-24 5 views
0

rspecの使用を開始したばかりなので、この仕様についてご意見をお寄せください。rspecをテストモデルに使用する方法のアドバイス

city_spec.rb

describe City do 

    before(:each) do 
    @city = Factory(:city) 
    end 

    describe "validation" do 
    it "valid" do 
     @city.should be_valid 
     @city.should have(:no).errors_on(:name) 
     @city.should have(:no).errors_on(:department_id) 
    end 

    it "has a unique name" do 
     c = Factory.build(:city, :name => @city.name) 
     c.should_not be_valid 
     c.name = 'unique' 
     c.should be_valid 
     # or via shoulda 
     c.should validate_uniqueness_of(:name) 
    end 

    it "belongs to department" do 
     c = Factory.build(:city, :department_id => nil) 
     c.should have(1).error_on(:department_id) 
     c.department_id = @city.department_id 
     c.should be_valid 
     c.should belong_to(:department) 
    end 
    end 
end 

department_spec: 私は2つのモデル

class City < ActiveRecord::Base 
    validates :name, :presence => true, :uniqueness => true 
    validates :department_id, :presence => true 
    belongs_to :department 
end 

class Department < ActiveRecord::Base 
    validates :name, :presence => true, :uniqueness => true 
    has_many :cities 
end 

は、私が検証と関係のステートメントを満たすために、これらのスペックを書いてい.rb

describe Department do 

    before(:each) do 
    @department = Factory(:department) 
    end 

    describe "validation" do 
    it "has a name" do 
    d = Factory.build(:department, :name => nil) 
    d.should_not be_valid 
    d.should have(1).error_on(:name) 
    d.name = 'good name' 
    d.should be_valid 
    end 

    it "has a unique name" do 
    d = Factory.build(:department, :name => @department.name) 
    d.should_not be_valid 
    d.name = 'good name' 
    d.should be_valid 
    end 

    it "has many cities" do 
    d = Factory.build(:department) 
    c1 = Factory.build(:city) 
    c2 = Factory.build(:city) 
    d.cities << c1 
    d.cities << c2 
    d.cities.size.should == 2 
    d.cities.first.should == c1 
    d.cities.last.should == c2 
    # or via shoulda 
    d.should have_many(:cities) 
end 
end 
end 

私はまたshoulda宝石を使用して見ることができますが、このアプローチは正しいと思いますか?私はこの機能のためにあまりにも多くのテストを書いた? ありがとうございます

答えて

0

モデルオブジェクトの状態をテストすることに焦点を当てています。それは完全にうまいです。私が見ている唯一のことは、これらのモデルのいずれかに関連する動作があるかどうかです。それらにはビジネスロジックを実装するメソッドが含まれていますか?もしそうでなければ、あなたは自宅で無料です、私は信じています。あなたがそうしたら、私はそれらの行動のテストを追加します。

+0

現在、私はビジネスロジックに関する方法はありません。私はrspecをテストしていました。私は正しい経路にいるかどうかを知るために、私が行ったことについてのフィードバックを求めていました。ありがとうございました –

0

私は間違いなくあなたのモデルをテストするためshouldaを示唆している:

あなたが宝石のファイルのテストグループにshoulda-マッチャーの宝石を使用する必要がありますRSpecのを使用する場合:

gem 'shoulda-matchers', :require => false 

あなたspec_helper.rbのアドオンでrequire 'rspec/rails'の後のrequire 'shoulda-matchers'。あなたが試験前の街のインスタンスを作成する必要がありますが、その後、あなたが使用する一意のテストのために

city_spec.rb

it { should validate_prescence_of(:name) } 
it { should validate_prescence_of(:department_id) } 
it { should belong_to(:department) } 

は、その後、あなたは、単に次のテストを使用します

it { should validate_uniqueness_of(:name) } 

あなたが見ることができます - あなたが書いたものよりはるかにクリーンで明確なテスト。

+0

あなたは "validates:account_id、:数値=> {:greater_then => 0}"をどのようにテストするでしょうか? – Nazgob

+1

@Nazgob私はテストを分割します(それはvalidate_numericality_of(:account_id)) 'それから'それはallow_value(0).for(:account_id)} ''それshould_not allow_value(-1) account_id) ' – nmott

+0

OK、1つの小切手でそれを持っているのはいいですね。 :) – Nazgob

関連する問題