测试Rails 3 Scopes有意义吗?

时间:2022-11-21 00:16:46

I'm a little torn. Do unit tests for Scopes in Rails 3 make sense?

我有点不知所措。 Rails 3中的Scopes的单元测试是否有意义?

On the one hand, I'm writing code and I should test that code.

一方面,我正在编写代码,我应该测试该代码。

However, on the other hand, basically all my scopes are effectively trivial. Checking one variable against a passed parameter is pretty much the most complex scope I have so far.

但是,另一方面,基本上我的所有范围都是微不足道的。根据传递的参数检查一个变量几乎是我迄今为止最复杂的范围。

scope :author, proc { |author| where(:author_user_id => author }

范围:作者,proc {|作者| where(:author_user_id => author}

That code is trivial and also more or less covered in the functions that actually USE the scopes.

该代码是微不足道的,并且或多或少地覆盖了实际使用范围的函数。

What are the best practices for testing or not testing scopes?

测试或不测试范围的最佳实践是什么?

2 个解决方案

#1


2  

If you think the scope is too simple to be tested, you can surely ignore it, but if you are thinking about testing scopes I'd tell you to look at this answer and focusing on testing behavior and not code itself.

如果您认为范围太简单而无法测试,您肯定会忽略它,但如果您正在考虑测试范围,我会告诉您查看此答案并专注于测试行为而不是代码本身。

#2


15  

David Chelimsky (Rspec's creator) offered up the following example in the Rspec Google Group:

David Chelimsky(Rspec的创建者)在Rspec Google Group中提供了以下示例:

describe User, ".admins" do 
  it "includes users with admin flag" do 
    admin = User.create! :admin => true 
    User.admin.should include(admin) 
  end

  it "excludes users without admin flag" do 
    non_admin = User.create! :admin => false 
    User.admin.should_not include(non_admin) 
  end 
end

class User < ActiveRecord::Base 
  named_scope :admins, :conditions => {:admin => true} 
end 

It's obviously not the same example as yours, but it should give you an idea of how to do it. The relevant thread for context is here: http://groups.google.com/group/rspec/browse_thread/thread/6706c3f2cceef97f

它显然与你的例子不同,但它应该让你知道如何做到这一点。上下文的相关主题如下:http://groups.google.com/group/rspec/browse_thread/thread/6706c3f2cceef97f

#1


2  

If you think the scope is too simple to be tested, you can surely ignore it, but if you are thinking about testing scopes I'd tell you to look at this answer and focusing on testing behavior and not code itself.

如果您认为范围太简单而无法测试,您肯定会忽略它,但如果您正在考虑测试范围,我会告诉您查看此答案并专注于测试行为而不是代码本身。

#2


15  

David Chelimsky (Rspec's creator) offered up the following example in the Rspec Google Group:

David Chelimsky(Rspec的创建者)在Rspec Google Group中提供了以下示例:

describe User, ".admins" do 
  it "includes users with admin flag" do 
    admin = User.create! :admin => true 
    User.admin.should include(admin) 
  end

  it "excludes users without admin flag" do 
    non_admin = User.create! :admin => false 
    User.admin.should_not include(non_admin) 
  end 
end

class User < ActiveRecord::Base 
  named_scope :admins, :conditions => {:admin => true} 
end 

It's obviously not the same example as yours, but it should give you an idea of how to do it. The relevant thread for context is here: http://groups.google.com/group/rspec/browse_thread/thread/6706c3f2cceef97f

它显然与你的例子不同,但它应该让你知道如何做到这一点。上下文的相关主题如下:http://groups.google.com/group/rspec/browse_thread/thread/6706c3f2cceef97f