博客

群星,我的归宿

Rspec中使用database_cleaner

这两天重新写Rspec测试,参考了 ruby-china 的测试用例和配置方法。

在使用Rspec的过程中因为有使用 FactoryGirl ,发现在一些用例中使用user的时候会发生

Validation failed: Email has already been taken (ActiveRecord::RecordInvalid)

因为在生成user的时候没有使用单例模式,所有每次向 FactoryGirl 要 user 的时候都会重新生成新的数据,所以就和之前发生冲突了。

参考 ruby-china 的测试代码,发现是使用了 database_cleaner 使得每个测试用例之后清扫了测试数据库,就不会引发冲突了。

1
2
3
4
5
6
  config.before(:each) do
    DatabaseCleaner.orm = :mongoid
    DatabaseCleaner.strategy = :truncation
    DatabaseCleaner.clean
    Rails.cache.clear
  end

但是当我把 database_cleaner 配置好并加到我项目 rspec 中的时候,却遇到了

/gems/database_cleaner-0.8.0/lib/database_cleaner/active_record/truncation.rb:62:in `<module:ConnectionAdapters>': superclass mismatch for class SQLite3Adapter (TypeError)

网上搜索一堆无效之后,转而使用另外一个解决方案

1
2
3
  config.after(:each) do
    ActiveRecord::Base.subclasses.each(&:delete_all)
  end

也就是在跑完每个测试用例之后,删除掉数据库中所有的 Model 数据

由于这样一段代码,接下来我遇到了一个更加头疼的问题。因为我使用 Devise 的 sign_in user 在每个测试用例中来模拟登陆后的用户,结果发现,前后两个测试用例,总是出现第一个用例能通过,第二个用例死活通过不了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class TeamsController < ApplicationController
  before_action :authenticate_user!
end

# 第一个 examples 能通过,第二个 examples 无法通过
describe TeamsController, :type => :controller do
  user = FactoryGirl.create(:user)

  describe ":index" do
    it "should show list of teams" do
      sign_in user
      get :index
      expect(response).to be_success
    end
  end

  describe ":new" do
    it "should allow access from authenticated user" do
      sign_in user
      get :new
      expect(response).to be_success
    end
  end

end

将第二个 examples 的 response.body 打印出来,发现

<html><body>You are being <a href="http://test.host/users/sign_in">redirected</a>.</body></html>

无解,然后分别打印两个用例的 controller.current_user,发现后一个是 nil

最终我将 database_cleaner 升级到了 1.3.0 重新bundle了一下,解决了 superclass mismatch for class SQLite3Adapter 的问题,移除了那段删除所有 Model 数据的 trick 代码所有的问题全部解决了。

Ruby 中的’小语法’

生成1到10的数组

1
array = (1..10).to_a

通过数组的each,直接调用对象的方法

1
ActiveRecord::Base.subclasses.each(&:delete_all)

记事

开学了

终于结束了4个月的 网易游戏平台开发工程师 的实习,本来一直要写总结的,一直等到了今天。期间要说收获,就是让自己又多了一大堆杂七杂八的项目经验,好在这些项目都是实实在在上线的而且每天都会有人使用的项目,每每想起,心中略微有些得意,比其他实习生要更有成就感一些。

Android Studio 开发中的Tips

入门Android时使用的一直是Eclipse,后来就转用Android Studio了,最近因为需要返回使用Eclipse,突然发现在编译打包一个apk的时候,Eclipse居然比Android Studio要快上不少!!

Android Studio build 速度太慢,这个太不科学了,google一下发现了以下解决方案:

使用最新的Android gradle plugin

build.gradle

    buildscript {
           dependencies {
                      classpath 'com.android.tools.build:gradle:0.9.+'       
                  }    
              }

使用最新的Gradle

gradle-wrapper.properties distributionUrl=http://services.gradle.org/distributions/gradle-1.8-all.zip

使用gradle deamon 后台任务,使用gradle parallel并行任务

gradle.properties

org.gradle.daemon=true    
org.gradle.parallel=true

开启deamon后,CPU起来了,这样就对了嘛,用机器时间减少人等待的时间,提高生产效率

几种GC的方式小结

标记清除方式

从根开始将可能被引用的对象用递归的方式进行标记,没有被标记的就当垃圾回收掉。

复制收集方式

因为标记清楚方式中有一个弊病就是在一次清除中,如果只有一小部分存活,消耗的时间会很多,所以复制收集方式应运而生。

引用计数方式

熟悉Objective-C开发iOS或者一些C++的开发者可能非常熟悉。

基本原理是,在每个对象中保存该对象的引用计数,当引用增加或者减少的时候对计数更新。

Flask and Rails Part 1

I have written flask recently,with a little project that I can study python.

I used to write Ruby on Rails, which is a very famous web framework and the most popular web framework all over the world.Rails is really really fast to develop most web products,MVC with RESTful design makes very thing easy. Based on meta-programming, the ActiveRecord makes data handles fly in the air.

But flask is also an awesome web framework after a few days since I write my project.It’s more like sinatra,which is a light ruby web framework.

Request handle

With rails,you need to handle a request URL at routes.rb

1
get blogs => blogs#index’

This code will handle /blogs GET method to a controller named blogs_controller.rb in app/controller/,and pass the params to function named index. After process the request,rails will render a view named index.html.erb in app/views/blogs/.

With flask,you need to handle a request URL at app.py

1
2
3
@app.route(/blogs)
def index():
    return render_template(index')

ORM

In my own words,ActiveRecord is much better than SQLAlchemy.

The Types of Associations in Rails:

  • belongs_to
  • has_one
  • has_many
  • has_many :through
  • has_one :through
  • has_and_belongs_to_many

Here are some data relationship implements of two ORM:

One to Many

With Rails:

1
2
3
4
5
6
7
class Customer < ActiveRecord::Base
  has_many :orders, dependent: :destroy
end

class Order < ActiveRecord::Base
  belongs_to :customer
end

Now you can get a customer’s orders by:

1
      @order = @customer.order.all()

Get an order’s customer by:

1
      @customer = @order.customer

With SQLAchemy:

1
2
3
4
5
6
7
8
class App(db.Model):
     __tablename__  = 'apps'
     owner_id       = db.Column(db.Integer, db.ForeignKey(users.id'))
     owner          = db.relationship('User', backref=db.backref('apps’))

class User(db.Model):
      __tablename__  =  'users'
       id            =  db.Column('id', db.Integer, primary_key=True)

Now you can get a user’s apps by:

1
      app = user.apps

Get an app’s owner by:

1
      user = app.owner

Many to Many

Although you need migration files in rails,since Flask and SQLAlchemy just configure data structure within model.The benefit of migration files comes out when you want to change your data structure of model.And now I finally known why my teacher ask me to design data structure at first,because other web framework have no ideas about what if our data structure changed.

With migration files in rails, you can change you data structure so easy that you can develop really really align.All details about the program becomes more and more clear after you start coding.