Category Archives: 测试

Rails Cookie测试

如果你在某个action中对Cookie进行了设置,然后你又打算对你设置的cookie进行测试,那么你可能会遇到麻烦,不妨看看下面这段代码: class CookiesController   def update     cookies[:name] = ‘demo’   end end describe CookiesController   it “should update cookies[:name] to ‘demo’” do     request.cookies[:name] = ‘test’     put :update     request.cookies[:name].should == ‘demo’   end end 如果你运行上面的测试,将会得到一个:expected ‘demo’ got ‘test’,这是怎么回事,cookie设置失败了?实际上这是正确的,之所以产生这样的结果是因为我们对Rails的Cookie机制不了解,首先我们需要区分request.cookies和Controller中的cookies,request.cookies是用于模拟客户端发送的 cookie的,它是一个Hash对象,因此我们可以像使用hash那样使用它: >> request.cookies[:name] = ‘test’ >> request.cookies[:name] test 但是Controller中的cookies则不同,它是一个ActionController::CookieJar对象,这个cookies对象实际上保存了2个cookie集合:incoming cookie和outgoing cookie。incoming cookie就是客户请求中所携带的cookie(也就是request.cookies),而outgoing cookie则是在请求处理完后将被设置回客户端的cookie。 因此尽管我们使用了[]=方法来设置cookie,但因为cookies对象重载了[]与[]=方法: []方法返回的是incoming cookie,也就是request中携带的cookie 而[]=方法设置的却是outgoing cookie,它只对下一个请求起作用,也就是说它会被包含在下一个请求的incoming cookie中。 因此设置controller cookies,我们只能得到: >> [...]

Posted in 测试 | Leave a comment

quietbacktrace: 让Test::Unit不再唐僧

如果你使用Test:Unit,那么你是否经常会为了它过于唐僧的错误输出而烦恼不已?就像下面这样: 1) Failure: test_should_be_create(ForumCommentsControllerTest) method assert_block in assertions.rb at line 48 method _wrap_assertion in assertions.rb at line 495 method assert_block in assertions.rb at line 46 method test_should_be_create in forum_comments_controller_test.rb at line 38 method __send__ in test_case_adapter.rb at line 19 method run in test_case_adapter.rb at line 19 method run in testsuite.rb at line 34 method each [...]

Posted in 测试 | Leave a comment

在test:unit中应用BDD

techno-weenie.net提供了两个插件simple_bdd和test_spec_on_rails,可以让你在test:unit中应用BDD而不需安装rspec,如果再加上mocha,test:unit与rspec就只存在语法的差异了。 安装: script/plugin install http://svn.techno-weenie.net/projects/plugins/simply_bdd/ script/plugin install http://svn.techno-weenie.net/projects/plugins/test_spec_on_rails/ 使用: 1. simple_bdd: context “New User” do   def setup   end   specify “should be invalid without a username” do   end end 这相当于: class NewUserTest < Test::Unit::TestCase   def setup   end   def test_should_be_invalid_without_a_username   end end test_spec_on_rails: #model @user.should.validate @user.should.be.validated #Redirection response.should.be.redirected # assert_response :redirect response.should.redirect foo_url(@foo) # assert_redirected_to foo_url(@foo) should.redirect_to [...]

Posted in 测试 | 3 Comments

RSpec进阶指南(三):Mocking

让我们先来看一个例子,假设我们要测试这个action: def comment   @post = Post.find(params[:id])   @comment = Comment.new(params[:comment])   respond_to do |format|     if @comment.save && @post.comments.concat(@comment)       flash[:notice] = ‘Comment was successfully updated.’       format.html { redirect_to post_path(@blogger, @post) }       format.xml { head k }     else       flash[:notice] = ‘Error saving comment.’       format.html { redirect_to post_path(@blogger, @post) }       format.xml { render ml => @comment.errors.to_xml }     end   end end [...]

Posted in 测试 | Leave a comment

RSpec进阶指南(2): 基础知识

下面是RSpec测试的一些基础语法,这些都是会运行通过的测试例: Strings: ‘foo’.should == ‘foo’ ‘foo’.should === ‘foo’ ‘foo’.should_not equal(‘foo’) ”.should be_empty ‘foo with bar’.should include(‘with’) ‘http://fr.ivolo.us’.should match(/http:\/\/.+/i) nil.should be_nil Numbers: 100.should < 200 200.should >= 100 (200 – 100).should == 100 # (100 – 80) is less than 21 100.should be_close(80,21) Arrays: [1,2,3].should have(3).items [].should be_empty [1,2,3].should include(2) Hashes: {}.should be_empty {:post => {:title [...]

Posted in 测试 | 1 Comment

RSpec进阶指南(1): rspec_scaffold

不同于前面那篇入门性的指南(如果还没读过,建议先阅读那篇文章然后再回来),这一系列将涵盖RSpec在Rails中的使用以及一些高级主题,来自我的同事Josh Stephenson,这是第一篇。 要使用RSpec来测试你的Rails应用,有一个比较简单的方法,使用rspec_scaffold生成器来代替scaffold_resource(Rails2.0中已更名为scaffold)。 $ ./script/generate rspec_scaffold post title:string body:text author:integer created_at:datetime updated_at:datetime      exists app/models/      exists app/controllers/      exists app/helpers/      create app/views/posts      create spec/controllers/      create spec/models/      create spec/helpers/      create spec/fixtures/      create spec/views/posts      create spec/controllers/posts_controller_spec.rb      create app/controllers/posts_controller.rb      create spec/helpers/posts_helper_spec.rb      create app/helpers/posts_helper.rb      create app/views/posts/index.rhtml      create app/views/posts/show.rhtml      create app/views/posts/new.rhtml      create app/views/posts/edit.rhtml      create app/models/post.rb      create spec/fixtures/posts.yml      create spec/models/post_spec.rb      create spec/views/posts/edit.rhtml_spec.rb      create [...]

Posted in 测试 | 1 Comment

Mock工具: Mocha还是FlexMock

简介 Mocha和FlexMock是两个比较流行的针对Rails单元测试的Mock工具, Mocha: 作者:James, Ben, Chris 以及 Paul 许可:MIT License SVN:svn://rubyforge.org/var/svn/mocha/trunk 文档:http://mocha.rubyforge.org/ FlexMock: 作者:Jim Weirich 许可:类BSD License SVN:svn://rubyforge.org/var/svn/mocha/trunk 文档:flexmock.rubyforge.org 安装 Mocha: $ gem install mocha $ script/plugin install svn://rubyforge.org/var/svn/mocha/trunk FlexMock: $ gem install flexmock 使用 Mocha: # Mocking a class method product = Product.new Product.expects(:find).with(1).returns(product) assert_equal product, Product.find(1) FlexMock: product = Product.new flexmock(Product).should_receive(:find).with(1).and_return(product) assert_equal product, [...]

Posted in 测试 | 3 Comments

使用SQLite in memory提高测试代码效率

对于需要进行持续集成的大型项目来说,运行测试代码的花费就显得尤为重要,Chris Roos在这篇帖子里介绍了一种通过使用SQLite数据库来 提高测试代码运行效率的方法。 首先你需要安装SQLite3以及sqlite3-ruby gem,然后修改database.yml: test: adapter: sqlite3 database: “:memory:” 但是这样有一个问题,由于SQLite数据库保存在内存中,因此必须在每次执行单元测试之前首先生成数据库的大纲,不过不用担心,Chris Roos已经提供了一段现成的代码,将它加到你的environment.rb就可以了: def in_memory_database?   ENV["RAILS_ENV"] == “test” and   ActiveRecord::Base.connection.class == ActiveRecord::ConnectionAdapters::SQLite3Adapter and   Rails::Configuration.new.database_configuration['test']['database'] == ‘:memory:’ end if in_memory_database?   puts “creating sqlite in memory database”   load “#{RAILS_ROOT}/db/schema.rb” # use db agnostic schema by default   # ActiveRecord::Migrator.up(‘db/migrate’) # use migrations end 不过要注意,以上代码最好加在”# Include your application configuration below”这一行之后,因为说不准后面的配置有可能就会用到数据库。 [...]

Also posted in 性能, 数据库 | Leave a comment

rcov: ruby代码覆盖率工具

简介 rcov是一个用于诊断Ruby代码覆盖率的工具,它最主要的用途就是用于确定单元测试是否覆盖到了所有代码,rcov使用一个经过优化的C运行时,因此性能相当惊人,同时它还提供多种格式的输出。 安装 $ gem install rcov 使用 rcov的使用相当简单: $ rcov test/*.rb 如果使用rspec,由于rcov不支持从命令行调用spec,因此你必须在测试代码中包含这一行: require ‘spec’ 下面的截图来自rcov的官方主页(Total coverage将空行也计算在内,而Code coverage则去除了空行): 你还可以通过链接直接查看单个文件的覆盖率: 更多信息参看这里。

Posted in 测试 | 1 Comment

RSpec简明指南

这是David Chelimsky写的一篇RSpec简明指南,原文在这里。 简介 要了解RSpec,我们首先需要了解什么是行为驱动开发(Behaviour Driven Development,简称BDD),BDD是一种融合了可接受性测试驱动计划(Acceptance Test Driven Planning),域驱动设计(Domain Driven Design)以及测试驱动开发(Test Driven Development,简称TDD)的敏捷开发模型。RSpec为BDD开发提供TDD支持。 你可以简单的将RSpec看作一个传统的单元测试框架,但我们更愿意将它看成是一种领域特定语言(Domain Specific Language,以下简称DSL),它的主要作用就是描述我们对系统执行某个样例(example)的期望行为(behavior)。 这篇指南遵从TDD思想,但是我们将使用行为(behavior)和样例(example)来代替测试例(test case)和测试方法(test method),想知道我们为什么采用这样的术语,请参看Dan North, Dave Astels, 以及 Brian Marick 的相关文章。 安装 目前RSpec的最新版本是1.0.5,需要Ruby184以上版本,可以通过下面这条命令安装: # gem install rspec 准备工作 整篇指南都围绕一个例子展开,因此在开始前,你最好先为这个例子建个目录: $ mkdir rspec_tutorial $ cd rspec_tutorial 开始 我们首先要了解的是RSpec DSL的”describe”与”it”方法,这两个方法有很多其它的名字(但是我们不推荐使用它们),我们之所以使用这样的命名,只是想让你站在行为(behavior)而不是结构(structure)的角度进行思考。 创建名为user_spec.rb的文件: describe User do end describe方法创建一个Behavior实例,所以你可以将”describe User”理解为”描述用户的行为(describe the behaviour of the User [...]

Also posted in 指南 | 5 Comments