---
title: "使用 RSpec 进行行为驱动测试,配置分析"
author: "java-admin"
date: "2011-11-11T13:21:28Z"
tags: []
categories: ["Web前端"]
slug: "rspec"
draft: false
lastmod: "2011-11-11T13:21:28Z"
id: 1253674
url: "https://java-admin.iteye.com/blog/1253674"
---

[http://huangzhimin.com/](http://huangzhimin.com/)

 

&lt;!-- AddThis Button BEGIN --&gt;

[](http://java-admin.iteye.com/admin/blogs/ "Send to Facebook_like")[](http://java-admin.iteye.com/admin/blogs/ "Tweet")[Tweet](http://twitter.com/share)[](http://java-admin.iteye.com/admin/blogs/ "Send to Google_plusone")[]()[]()[1](http://java-admin.iteye.com/admin/blogs/ "View more services")

&lt;script src="http://s7.addthis.com/js/250/addthis\_widget.js#pubid=flyerhzm" type="text/javascript"&gt;&lt;/script&gt;&lt;!-- AddThis Button END --&gt;

# [after\_commit](http://java-admin.iteye.com/2011/11/06/after_commit/ "after_commit")

We are using RabbitMQ as our message queue system, ruby client is workling. This week we encountered a strange issue, we create a notification, and define an after\_create callback to ask workling to find that notification and then push the notification to twitter or facebook, it works fine except that sometimes it will raise an error said "can't find the notification with the specified ID"

```
class Notification < ActiveRecord::Base
  after_create :asyns_send_notification
  ......
  def async_send_notification
    NotificationWorker.async_send_notification({:notification_id => id})
  end
end

class NotificationWorker < Workling::Base
  def send_notification(params)
    notification = Notification.find(params[:notification_id])
    ......
  end
end
```

It's wierd the notification\_id is passed to NotificationWorker, that means the notification is already created, the notification is supposed to be existed.

After talking with MySQL DBA, we find the problem is the find sql is executed before insert transaction is committed.

Let me describe it step by step.

1. Notification sends "Transaction Begin" command
2. Notification sends "INSERT" command
3. Notification gets "next sequence value" as new object id
4. Notification sends "new object id" to NotificationWorker
5. NotificationWorker sends "SELECT" command to find notification object
6. Notification 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})
  end
end
```

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

Posted in  [Rails](http://java-admin.iteye.com/categories/Rails "Rails")

[1 Comment](http://java-admin.iteye.com/2011/11/06/after_commit/#disqus_thread)

06 Nov 2011

* * *

# [reset\_counter in rails](http://java-admin.iteye.com/2011/10/31/reset_counter-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)
  end
end
```

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](http://java-admin.iteye.com/categories/Rails "Rails")

[0 Comments](http://java-admin.iteye.com/2011/10/31/reset_counter-in-rails/#disqus_thread)

31 Oct 2011

* * *

# [use rspec filter to speed up tests](http://java-admin.iteye.com/2011/10/21/rspec-filter/ "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 =&gt; true can be applied on describe/context as well.

One problem is that if there is no :focus =&gt; 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.