rails 的测试的 bug 当你的 model 的 after_create 中有raise 时

app/models/part.rb


class Part < ActiveRecord::Base
def after_create
raise "no way" if self.name == "test"
end
end


app/controllers/parts_controller.rb


class PartsController < ApplicationController
def test
Part.create!(:name => "test")
end
end


test/functional/parts_controller_test.rb


require File.dirname(__FILE__) + '/../test_helper'

class PartsControllerTest < ActionController::TestCase
def test_test
old_count = Part.count
post :test rescue nil
assert_equal(old_count, Part.count)
end
end



ruby test\functional\parts_controller_test.rb


结果是


<2> expected but was
<3>.


日志是

Processing PartsController#test (for 0.0.0.0 at 2008-05-01 16:26:57) [POST]
Session ID:
Parameters: {"action"=>"test", "controller"=>"parts"}
[4;35;1mPart Create (0.000000)[0m [0mINSERT INTO parts ("name", "updated_at", "created_at") VALUES('test', '2008-05-01 16:26:57', '2008-05-01 16:26:57')[0m
[4;36;1mSQL (0.000000)[0m [0;1mSELECT count(*) AS count_all FROM parts [0m

看到没. 居然没有 回滚

换用集成测试
test/integration/model_hg_test.rb

require "#{File.dirname(__FILE__)}/../test_helper"

class ModelHgTest < ActionController::IntegrationTest
fixtures :parts

def test_truth
old_count = Part.count

puts Part.find_by_sql("select * from parts").collect(&:id).join(", ")
puts Part.find_by_sql("select * from parts").collect(&:name).join(", ")

post '/parts/test'

puts Part.find_by_sql("select * from parts").collect(&:id).join(", ")
puts Part.find_by_sql("select * from parts").collect(&:name).join(", ")

assert_equal(old_count, Part.count)

end
end


结果是



D:\t1\t1>ruby test\integration\model_hg_test.rb
Loaded suite test/integration/model_hg_test
Started
953125641, 996332877
MyString, MyString
953125641, 996332877, 996332878
MyString, MyString, test
F
Finished in 1.281 seconds.

1) Failure:
test_truth(ModelHgTest)
[test/integration/model_hg_test.rb:17:in `test_truth'
E:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/integration.rb:547:in `
']:
<2> expected but was
<3>.

1 tests, 1 assertions, 1 failures, 0 errors


没有 回滚

狂汗!!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值