【发布时间】:2013-01-03 21:58:10
【问题描述】:
作为 TDD 和 Rails 领域的新手,我正试图弄清楚如何减少我的规范的运行时间。
我放弃了 Rails 框架的加载,只加载了 ActiveRecord 部分。这有很大帮助(-8s),但我仍然想知道我是否还能做更多。
spec 文件有 5 个示例,使用 time rspec path_to_spec.rb 运行它需要 1.7 秒。当我删除 FactoryGirl 并改用 Project.create 时,我到了 1.4 秒。
我要测试的是模型的范围和查询组合是否正常。我不确定在这种情况下是否可以使用模拟/存根。
- 有没有一种方法可以利用模拟/存根功能来测试
next_project的行为? - 我如何知道 ActiveRecord 交互时间的限制是多少?我的意思是,在测试 ActiveRecord 模型时,我必须处理 1-2 秒的执行时间吗?
- 关于如何加快速度的任何其他建议?
我正在使用 ruby 1.9.3、rspec 2.1.12、ActiveRecord 3.2.9
我的模型文件:
class Project < ActiveRecord::Base
default_scope where(:is_enabled => true).order(:position)
def self.by_slug(slug)
where(:slug => slug).first
end
def find_neighbors
Neighbors.new(previous_project, next_project)
end
private
def previous_project
Project.where("position < ?", position).last
end
def next_project
Project.where("position > ?", position).first
end
end
Neighbors = Struct.new(:previous, :next)
我的规格文件:
require 'active_record'
require 'yaml'
require 'factory_girl'
require './app/models/project'
dbconfig = YAML::load(File.open('./config/database.yml'))
ActiveRecord::Base.establish_connection(dbconfig["test"])
FactoryGirl.define do
factory :project do
slug { name.parameterize }
sequence(:position, 1)
is_enabled true
end
end
describe Project do
before(:all) do
@first_project = FactoryGirl.create(:project, name: "First Project")
@second_project_disabled = FactoryGirl.create(:project, name: "Second Project", is_enabled: false)
@third_project = FactoryGirl.create(:project, name: "Third Project")
@fourth_project_disabled = FactoryGirl.create(:project, name: "Fourth Project", is_enabled: false)
@fifth_project = FactoryGirl.create(:project, name: "Fifth Project")
end
after(:all) do
projects = [@first_project, @second_project_disabled, @third_project, @fourth_project_disabled, @fifth_project]
projects.each { |p| p.delete }
end
context "when requesting a project by URL slug" do
it "should return that project if it is enabled" do
Project.by_slug(@third_project.slug).should eql(Project.find(@third_project.id))
end
it "should not return the project if it is not enabled" do
Project.by_slug(@fourth_project_disabled.slug).should be_nil
end
end
context "when getting first project" do
it "should have a reference only to the next project enabled" do
neighbors = @first_project.find_neighbors
neighbors.previous.should be_nil
neighbors.next.should eql(Project.find(@third_project.id))
end
end
# 2 more examples similar to the last one
end
【问题讨论】:
-
我使用 Zeus 来节省引导导轨的开销。
-
@FrederickCheung 我正在使用 spork 来启动 rails,但发现它没有帮助。所以我暂时摆脱了它。不过,我不知道与 Zeus 的性能比较。
标签: ruby-on-rails ruby performance model rspec