testing with mock object

41
使 Mock object

Upload: wear

Post on 19-Jan-2015

3.936 views

Category:

Technology


9 download

DESCRIPTION

shanghaionrails first event presentation by zhanyuanyi

TRANSCRIPT

Page 1: Testing with mock object

使 用 Mock object进 行 测 试

张 元 一

Page 2: Testing with mock object

什么是Mock object?

Page 3: Testing with mock object

Mock object 是面向对象编程中,对真实对象的行为以可控的方式进行模拟的一种虚拟对象

wikipedia

Page 4: Testing with mock object

汽车碰撞测试中的模型人

Page 5: Testing with mock object

为什么需要Mock object?

Page 6: Testing with mock object

1.降低代码耦合

Page 7: Testing with mock object

一个改动导致大量测试失败

Page 8: Testing with mock object

def test_createpost :create, :user =>

{:name => 'test'}end

def test_updateput :update, :user =>

{:name => 'new'}end

Page 9: Testing with mock object

add_column :first_nameadd_column :last_nameremove_column :name

挂了!

Page 10: Testing with mock object

2. 人生短暂,珍惜时间

Page 11: Testing with mock object

不要重复测试

A测试例中已经测试过的代码没必要再在B测试例中进行测试,尤其是这些代码很耗时

Page 12: Testing with mock object

# post_test.rbdef test_should_create_postpost = Post.new(...) assert post.valid?

end

class postvalidates_presence_of :xxx

end

Page 13: Testing with mock object

# posts_controller_test.rbdef test_should_create_postpost :create,

:post => {...}...

end

Page 14: Testing with mock object

重复了!

# posts_controller.rbdef create

post = Post.new(params[:post]) if post.save

...end

Page 15: Testing with mock object

3.让自己更轻松

Page 16: Testing with mock object

等待是人世间最痛苦的事情之一,尤其是你苦苦等待的结果居然是:

Failure!

Page 17: Testing with mock object

如何使用Mock object?

Page 18: Testing with mock object

Mocha, Flex Mock or RSpec

Page 19: Testing with mock object

Mocha

Page 20: Testing with mock object

@post = mock(“post”)

@post = Post.new

Page 21: Testing with mock object

@post = mock(“post”) @post.digg

#<Mock:post>.digg -expected calls: 0, actual calls: 1

Page 22: Testing with mock object

@post.expects(:digg)

@post.instance_eval {def digg...

end}

Page 23: Testing with mock object

def [email protected](:digg)

end

#<Mock:post>.digg -expected calls: 1, actual calls: 0

Page 24: Testing with mock object

@post.expects(:digg).once

at_least(min) at_least_onceat_most(max) at_most_oncenevertimes(num)

Page 25: Testing with mock object

if @post.digg # nil...

else...

end

Page 26: Testing with mock object

@post.expects(:digg) .returns(true)

@post.expects(:digg) .raises(exception)

Page 27: Testing with mock object

@post.digg(@blocked) # [email protected](@unblocked) # true

@post.expects(:digg) .with(any_of(User.blocked)) .returns(false)

@post.expects(:digg) .with(any_of(User.unblocked))

.returns(true)

Page 28: Testing with mock object

all_ofany_ofanythinghas_entry(key, value) has_key(key) has_value(value) includes(item) instance_of(klass) kind_of(klass) regexp_matches(regexp)

Page 29: Testing with mock object

@post.stubs(:method) @post.stubs(:method =>

:result)

@post.expects(:method) .at_least(0)

@post.expects(:method) .at_least(0) .returns(:result)

Page 30: Testing with mock object

@post = stub_everything('post' :method => :result)

@post.method1 # [email protected] # [email protected] # :result

Page 31: Testing with mock object

Mocha on Rails

Page 32: Testing with mock object

def test_createpost :create, :user =>

{:name => 'test'}end

add_column :first_nameadd_column :last_nameremove_column :name

Page 33: Testing with mock object

def test_should_create_userPost.expects(:new)

.returns(@post) @post.expects(:save)

.returns(true)

post :create, :user => {}assert_redirect_to

user_path(@user) end

Page 34: Testing with mock object

告别Fixture!

Page 35: Testing with mock object

# teachers_students.ymlone: teacher_id: 1student_id: 1

two:teacher_id: 1student_id: 2

three:teacher_id: 2student_id: 3

不够直观

浪费时间

Page 36: Testing with mock object

def test_should_show_postUser.expects(:find)

.returns(@user) @user.posts.expects(:find)

.returns(@post1)

get :show, :id=>1, :user_id=>1assert_response :success

end

def setup@user = User.new(:name=>'test') @post1 =

Post.new(:title=>'post1') end

Page 37: Testing with mock object

RSpec

Page 38: Testing with mock object

it “should create a new user” doUser.should_receive(:new)

.and_return(@user) User.stub!(:save)

.and_return(true)

post :create, :user => {}response.should

redirect_to(user_path(@user)) end

Page 39: Testing with mock object

同样的思想,不同的实现!

Flex Mock?

Page 40: Testing with mock object

问题?

Page 41: Testing with mock object

谢谢!