首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

施用 RSpec 进行行为驱动测试,配置分析

2012-08-26 
使用 RSpec 进行行为驱动测试,配置分析http://huangzhimin.com/?!-- AddThis Button BEGIN --Notificati

使用 RSpec 进行行为驱动测试,配置分析

http://huangzhimin.com/

?

<!-- AddThis Button BEGIN -->Notification sends "INSERT" commandNotification gets "next sequence value" as new object idNotification sends "new object id" to NotificationWorkerNotificationWorker sends "SELECT" command to find notification objectNotification sends "Transaction Commit" command

As you seen, at step 5, the new notification is not existed in the mysql database yet, so the error "Not found" will be raised.

To solve this issue, we can use after_commit callback.

In rails 2.x, we should install after_commit gem, in rails 3.x, after_commit callback is supported by default.

class Notification < ActiveRecord::Base  after_commit_on_create :asyns_send_notification  ......  def async_send_notification    NotificationWorker.async_send_notification({:notification_id => id})  endend

So Notification asks NotificationWorker to run only after the transaction is committed.

Posted in? Rails

reset_counter in rails

I thought reset_counter method is to reset a counter_cache column to be 0, but it is not. After trying several times, I finally realize that reset_counter is to update the value of counter_cache column to the exact count of associations. The usecase of reset_counter is when you add the counter_cache in migration and update the counter_cache value, like

def self.up  add_column :posts, :comments_count  Post.all.each do |post|    Post.reset_counter(post.id, :comments)  endend

it will add comments_count column to posts table, and calculate the comments count for each post, and set it to posts' comments_count column.

I didn't find a method to reset the counter_cache column to be 0, why? Because counter_cache is used to cache the association count, it will be incremented and decremeneted automatically, you should never reset it 0. If you find you need to reset counter_cache to 0, that means it's a wrong usage of counter_cache.

Posted in? Rails

use rspec filter to speed up tests

Rspec 2 introduce a very efficient way to test only one test or one test suit, it's filter_run.

You should first add filter_run in rspec/spec_helper.rb

config.filter_run :focus => true

Then you can tell rspec to test only one test you are focused by

it "should focus now", :focus => true do  ...end

rspec will only test this spec, :focus => true can be applied on describe/context as well.

One problem is that if there is no :focus => true on your tests, rspec will do nothing, but most of time we are expecting to test all specs if no focus is true, so you should add a line to spec_helper as well.

config.run_all_when_everything_filtered = true

As the name implies, rspec will test all specs if no focus filter.

Another you may interest that you can also define filter_run_excluding

config.filter_run_excluding :slow => true

rspec will run all specs except what specs are marked as slow.

热点排行