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
没有 回滚
狂汗!!!
分享到:
- 2008-05-01 16:39
- 浏览 1540
- 评论(7)
- 论坛回复 / 浏览 (7 / 3532)
- 查看更多
相关推荐
自述文件版本和设置$ ruby -vruby 2.6.1p33 (2019-01-30 revision 66950) [x86_64-darwin18]$ rails -... $ rails new rails_model_test_hello_world -T -m ~/rtfb_template.rb$ cd rails_model_test_hello_world$ rail
Rails_Recipes_with_Source_Code
介绍插件,用于对记录进行排序(使用 gem)安装要启用rails_admin_acts_as_list,请将以下内容添加到您的Gemfile : gem 'rails_admin_acts_as_list'gem 'rails_admin' 重要提示: rails_admin_acts_as_list之前必须...
应用Rails进行REST 开发 ,翻译自《RESTful Rails Development》
使用Rails4Devise和Grape验证API_JavaScript_CSS_下载
Ruby on Rails Guides v2 - Ruby on Rails 4.2.5
css和图片 博文链接:https://hideto.iteye.com/blog/197517
一个从图像生成引导配色方案的 Rails 应用程序
Rails 的英文脏话过滤器 这个插件将允许您使用基本替换或字典术语过滤亵渎。
InspiniaAdmin 2.6.1 Rails_Full_Version
创建新项目时通常使用的宝石和配置样板。 最新的Rails版本。 PostgreSQL Webpack作为资产构建器 React Redux … 安装说明 $ git clone git@github.com:bigardone/rails_with_webpack_starter_kit.git $ bundle ...
[Ruby_on_Rails][中文][Rails_5.x]__03-2_._安裝不同版本的Rails與產生Rails_5.x版
这个 gem 是用于测量gem 的 Rails 集成。 它提供了 ActiveRecord 适配器,用于保存和检索测量值及其单位和模型验证。 用法 活动记录 列应具有_valueand_unit后缀,并且是DECIMALand VARCHAR,并且接受默认值。支持...
复杂Rails系统架构优化
ruby_on_rails_3_入门教程(中文)
factory_bot_rails 用简单的定义语法替换了固定装置,支持多种构建策略(保存的实例,未保存的实例,属性哈希和存根对象),并且支持同一类的多个工厂( user , admin_user等),包括工厂继承。 从factory_girl_...
AtomicFirstOrCreate ActiveRecord first_or_create替代方案,它在RecordNotUnique异常上重试。 first_or_create本身不能保证唯一性...正如Rails团队所记录的那样,诸如first_or_create或find_or_create方法不是原子
agile_web_development_with_rails_3rd_edition.9994652073.pdf
RestFul_Rails_Dev_v_0.1 RestFul_Rails_Dev_v_0.1
Windows7_Cygwin_Git_RVM_Ruby1.9.3_Rails3_MongoD B_Nginx_Unicorn_Rspec_Guard_Spork(2-Ruby on Rails3 安装配置