2011-07-24 48 views
0

我刚开始使用rspec,我希望对此规范有意见。 我有2种型号关于使用rspec进行测试的建议模​​型

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 

我为了满足验证和关系的语句写下那些规格:

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 .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

我强烈建议你早该来测试机型:

当使用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

如何使用shoulda测试“validates:account_id,:numericality => {:greater_then => 0}”? – Nazgob

+1

@Nazgob我会拆分测试'it {should validate_numericality_of(:account_id)}'然后'it {should allow_value(0).for(:account_id)}''it_not allow_value(-1).for(: account_id)' – nmott

+0

好吧,这将是很好,它在一个检查,虽然,应该拉请求的想法? :) – Nazgob