Testing with mock object

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

1 comments

Comments 1 - 1 of 1 previous next Post a comment

  • + guest47fcf0 guest47fcf0 3 years ago
    人生短暂,哪一个实现您最会向读者推荐。

Post a comment
Embed Video
Edit your comment Cancel

3 Favorites

Testing with mock object - Presentation Transcript

  1. 使 用 Mock object 进 行 测 试 张 元 一
  2. 什么是Mock object?
  3. wikipedia Mock object 是面向对象编程中 ,对真实对象的行为以可控的 方式进行模拟的一种虚拟对象
  4. 汽车碰撞测试中的模型人
  5. 为什么需要Mock object?
  6. 1.降低代码耦合
  7. 一个改动导致大量测试失败
  8. def test_create post :create, :user => {:name => 'test'} end def test_update put :update, :user => {:name => 'new'} end
  9. add_column :first_name add_column :last_name remove_column :name 挂了!
  10. 2. 人生短暂,珍惜时间
  11. 不要重复测试 A测试例中已经测试过的代 码没必要再在B测试例中进 行测试,尤其是这些代码很耗 时
  12. # post_test.rb def test_should_create_post post = Post.new(...) assert post.valid? end class post validates_presence_of :xxx end
  13. # posts_controller_test.rb def test_should_create_post post :create, :post => {...} ... end
  14. # posts_controller.rb def create post = Post.new(params[:post]) if post.save ... end 重复了!
  15. 3.让自己更轻松
  16. 等待是人世间最痛苦的事情之一, 尤其是你苦苦等待的结果居然是: Failure!
  17. 如何使用Mock object?
  18. Mocha, Flex Mock or RSpec
  19. Mocha
  20. @post = mock(“post”) @post = Post.new
  21. @post = mock(“post”) @post.digg #<Mock:post>.digg - expected calls: 0, actual calls: 1
  22. @post.expects(:digg) @post.instance_eval { def digg ... end }
  23. def test_xxx @post.expects(:digg) end #<Mock:post>.digg - expected calls: 1, actual calls: 0
  24. @post.expects(:digg).once at_least(min) at_least_once at_most(max) at_most_once never times(num)
  25. if @post.digg # nil ... else ... end
  26. @post.expects(:digg) .returns(true) @post.expects(:digg) .raises(exception)
  27. @post.digg(@blocked) # false @post.digg(@unblocked) # true @post.expects(:digg) .with(any_of(User.blocked)) .returns(false) @post.expects(:digg) .with(any_of(User.unblocked)) .returns(true)
  28. all_of any_of anything has_entry(key, value) has_key(key) has_value(value) includes(item) instance_of(klass) kind_of(klass) regexp_matches(regexp)
  29. @post.expects(:method) .at_least(0) @post.expects(:method) .at_least(0) .returns(:result) @post.stubs(:method) @post.stubs(:method => :result)
  30. @post = stub_everything('post' :method => :result) @post.method1 # nil @post.method2 # nil @post.method # :result
  31. Mocha on Rails
  32. def test_create post :create, :user => {:name => 'test'} end add_column :first_name add_column :last_name remove_column :name
  33. def test_should_create_user Post.expects(:new) .returns(@post) @post.expects(:save) .returns(true) post :create, :user => {} assert_redirect_to user_path(@user) end
  34. 告别Fixture!
  35. # teachers_students.yml one: teacher_id: 1 不够直观 student_id: 1 two: teacher_id: 1 浪费时间 student_id: 2 three: teacher_id: 2 student_id: 3
  36. def setup @user = User.new(:name=>'test') @post1 = Post.new(:title=>'post1') end def test_should_show_post User.expects(:find) .returns(@user) @user.posts.expects(:find) .returns(@post1) get :show, :id=>1, :user_id=>1 assert_response :success end
  37. RSpec
  38. it “should create a new user” do User.should_receive(:new) .and_return(@user) User.stub!(:save) .and_return(true) post :create, :user => {} response.should redirect_to(user_path(@user)) end
  39. Flex Mock? 同样的思想,不同的实现!
  40. 问题?
  41. 谢谢!

+ wearwear, 3 years ago

custom

3190 views, 3 favs, 1 embeds more stats

shanghaionrails first event presentation by zhanyua more

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 3190
    • 3189 on SlideShare
    • 1 from embeds
  • Comments 1
  • Favorites 3
  • Downloads 75
Most viewed embeds
  • 1 views on http://localvencorps

more

All embeds
  • 1 views on http://localvencorps

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories