关于使用 rspec 测试模型的建议

发布于 2024-11-26 07:57:59 字数 2234 浏览 1 评论 0原文

我刚刚开始使用 rspec,我想听听您对此规范的看法。 我有 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

我写下了这些规格,以满足验证和关系声明:

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 gem,你认为这种做法正确吗?我为这个功能写了太多测试? 谢谢

I just started using rspec and I'd like to have your opinion about this spec.
I have 2 models

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

and

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

I wrote down those spec in order to satisfy the validation and relationship statement:

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

has you can see I used also shoulda gem, do you think this approach is correct? I wrote too much test for this functions?
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨笨の傻瓜 2024-12-03 07:57:59

乍一看,您似乎专注于测试模型对象的状态。那完全没问题。我唯一要考虑的是这些模型是否有任何相关的行为?它们是否包含任何实现业务逻辑的方法?如果没有,我相信你就可以回家了。如果你这样做,那么我会为这些行为添加测试。

At a quick glance it appears you're focusing on testing state of the model objects. That's completely fine. The only thing I'd look at is do either of these models have any associated behavior? Do they contain any methods that implement business logic? If not, then you're home free, I believe. If you do, then I'd add tests for those behaviors.

拿命拼未来 2024-12-03 07:57:59

我绝对建议使用 shoulda 来测试你的模型:

当使用 rspec 时,你应该在 gem 文件的测试组中使用 shoulda-matchers gem:

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

在你的 spec_helper.rb 中添加 require 'shoulda-matchers' 之后需要“rspec/rails”

然后,您只需使用以下测试:

city_spec.rb

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

对于唯一性测试,您需要在测试之前创建一个 city 实例,但随后您将使用:

it { should validate_uniqueness_of(:name) }

正如您所看到的 - 比您编写的测试更干净、更清晰。

I would definitely suggest shoulda for testing your models:

When using rspec you should use shoulda-matchers gem in the test group of your gem file:

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

In your spec_helper.rb add the require 'shoulda-matchers' after require 'rspec/rails'.

Then you would simply use the following tests:

city_spec.rb

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

For the uniqueness test you need to create an instance of city before the test but then you would use:

it { should validate_uniqueness_of(:name) }

As you can see - much cleaner and clearer tests than what you have written.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文