how to test observe with rspec in ruby on rails.
2011-01-01 16:38
706 查看
在测试 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.
试着写了一下,觉得还不错,记录一下。
测试代码
1 require 'spec_helper' 2 3 describe ContactInfoObserver do 4 before(:each) do 5 @phone = "15812345678" 6 @obs = ContactInfoObserver.instance 7 end 8 9 it ' should be change contact my user id when contact registered' do 10 user = mock_model(UserInfo) 11 UserInfo.stub(:find_by_phone).and_return(user) 12 m = mock_model(ContactInfo, :phone => @phone) 13 m.should_receive("my_user_id=").with(user.id) 14 @obs.before_create m 15 end 16 17 it ' should not change contact my user id when contact unregistered' do 18 UserInfo.stub(:find_by_phone).and_return(false) 19 m = mock_model(ContactInfo, :phone => @phone) 20 m.should_not_receive("my_user_id=") 21 @obs.before_create m 22 end 23 24 end
observer
1 class ContactInfoObserver < ActiveRecord::Observer 2 def before_create(contact_info) 3 #contact_info.logger.error("model "+contact_info.inspect) 4 user = UserInfo.find_by_phone contact_info.phone 5 puts "model observer"+user.inspect 6 if user 7 puts 'at here' 8 contact_info.my_user_id= user.id 9 end 10 end 11 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