test_helpful released -- A plugin on rails to make test easier

该插件为 Rails 应用提供了一套测试辅助方法,包括功能测试支持矩阵、简化测试初始化过程及重用测试代码等功能。适用于 Rails 2.0 边缘版本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I extract the plugin from a real project http://code.google.com/p/railsjob/ to make test easier.

I submit it to http://agilewebdevelopment.com/plugins/test_helpful

And write a short document -- sorry for my ugly English. :p

TestHelpful
===========

This plugin provide the following helper method for test

# Functional Matrix Test support

To knowlearn about it: http://www.infoq.com/news/2007/04/matrix-your-tests
This plugin modify the matrix test code of ZenTest and add some new feature.

* You need not to define matrix_init_xxx method, but init with block:

In your test case:

def setup
  init(:logined){ login_as :dongbin}
end

then "login_as :dongbin" will be called when setup include "logined"

* Reuse your test with after_filter

  In your test case

  after_filter :render_form, :new, :edit

  def matrix_test_render_form
    assert_response :success
    assert assigns(:job)
    assert_form(assigns(:job), *@@job_default_attr.keys)
  end

  def matrix_test_new
    assert_template 'new'
  end

  def matrix_test_edit
    assert_template 'edit'
  end

  Then matrix_test_render_form will be called after  matrix_test_new or matrix_test_edit

# assert_form

  Note: This method can only work with edge rails(Rails 2.0)

  assert_form(@user, :email, :password)

  equals to

  assert_select("form[action=/users]") do
    assert_select("#user_email[name=?]", "user[email]")
    assert_select("#user_password[name=?]", "user[password]")
    assert_select("input[type=submit]")
  end

# Add a method named "create" for unit tests and functional tests

* For Unit Test

in UserTest

      create(:name => 'dongbin')

equals to

      User.create(@@user_default_attr.merge(:name => 'dongbin')

If @@user_default_attr not defined, then it is an empty {}

* For functional test

In UserControllerTest

  create(:name => 'dongbin')

equals to

  post :create, :user => @@user_default_attr.merge(:name => 'dongbin')

If @@user_default_attr not defined, then it is an empty {}

Example

http://railsjob.googlecode.com/svn/trunk/test/functional/jobs_controller_test.rb

Copyright (c) 2007 Bin Dong<dongbin.cn@gmail.com>, released under the MIT license

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值