how to test observe with rspec in ruby on rails.
2011-01-01 16:38
519 查看
在测试 model时,有一个改动是observe, 我想一把测试了,可是这个observer在rspec中一直不肯执行. 但在console下的test env没有任何问题。
哥花了一天的时间来调试,终于还是没有搞定,有遇到此类的问题的朋友可以协助我一下,谢谢。
用我的垃圾英语在rails的mail list上问了一下,说要我看下面的东东
http://stackoverflow.com/questions/33048/how-would-you-test-observers-with-rspec-in-a-ruby-on-rails-application
上面主要还是推荐observer和model分开来测试,在observer中model的构建用mock.
试着写了一下,觉得还不错,记录一下。
测试代码
observer
哥花了一天的时间来调试,终于还是没有搞定,有遇到此类的问题的朋友可以协助我一下,谢谢。
用我的垃圾英语在rails的mail list上问了一下,说要我看下面的东东
http://stackoverflow.com/questions/33048/how-would-you-test-observers-with-rspec-in-a-ruby-on-rails-application
上面主要还是推荐observer和model分开来测试,在observer中model的构建用mock.
试着写了一下,觉得还不错,记录一下。
测试代码
require 'spec_helper' describe ContactInfoObserver do before(:each) do @phone = "15812345678" @obs = ContactInfoObserver.instance end it ' should be change contact my user id when contact registered' do user = mock_model(UserInfo) UserInfo.stub(:find_by_phone).and_return(user) m = mock_model(ContactInfo, :phone => @phone) m.should_receive("my_user_id=").with(user.id) @obs.before_create m end it ' should not change contact my user id when contact unregistered' do UserInfo.stub(:find_by_phone).and_return(false) m = mock_model(ContactInfo, :phone => @phone) m.should_not_receive("my_user_id=") @obs.before_create m end end
observer
class ContactInfoObserver < ActiveRecord::Observer def before_create(contact_info) #contact_info.logger.error("model "+contact_info.inspect) user = UserInfo.find_by_phone contact_info.phone puts "model observer"+user.inspect if user puts 'at here' contact_info.my_user_id= user.id end end end
相关文章推荐
- how to test observe with rspec in ruby on rails.
- How to Use Memcached With Ruby on Rails on Ubuntu 12.04 LTS
- How To Use MySQL with Your Ruby on Rails Application on Ubuntu 14.04
- How to install Ruby on rails in Windows 2003/xp 如何安装ROR
- How to Generate PDF in Ruby on Rails(HowtoGeneratePDFs) ZT
- How to install Ruby on Rails in Ubuntu 11.10
- How to fix the “msvcrt-ruby18.dll is missing from your computer” error message in Ruby on Rails
- How to connect DB2 with ruby on rails 如何连接DB2数据库
- How to Install Xcode, Homebrew, Git, RVM, Ruby & Rails on Snow Leopard, Lion, Mountain Lion, and Mavericks
- What 's CATT(Computer Aided Test Tool)?How to deal with the CATT in the SAP System?什么是CATT(计算机附加测试工具)
- How to read out WhatsApp messages with Tasker and react on their content in real time
- how to change the original ruby version in Rails app
- Ruby on Rails Session 2: How to install Aptana Studio 3 on Ubuntu 12.04 LTS
- [转] How to kill ruby on rails WEBrick server daemon
- How to debug test code on Windows Mobile Emulator with Visual Studio
- How to Test Controller Concerns in Rails 4
- (转)How to Install Xcode, Homebrew, Git, RVM, Ruby & Rails on Snow Leopard, Lion, and Mountain Lion
- How to use ActiveRecord in a ruby script outside Rails?
- How to deal with "Could not find component on update server. Contact VMware Support or your system administrator." in Vmware.
- How To Install Ruby with rbenv on Cent OS 7.2