0
Moq 的進階用法
Bryan Lin
2014/03/10
Agenda
• Linq to Mocks
• Methods
• Matching Arguments
• Properties
• Events
• Callbacks
• Verification
• Customizing Mock ...
Linq to mocks
• Moq is the one and only mocking framework that allows specifying mock
behavior via declarative specificati...
Linq to mocks
• Linq to Mocks is great for quickly stubbing out dependencies that typically
don't need further verificatio...
Methods
• You can also mock objects by method call
Methods
Methods
Matching Arguments
Properties
• Setup a property so that it will automatically start tracking its value (also
known as Stub)
Properties
• Stub all properties on a mock (not available on Silverlight):
mock.SetupAllProperties();
Events
Callbacks
Callbacks
Verification
Verification
Customizing Mock Behavior
• Make mock behave like a "true Mock", raising exceptions for anything that
doesn't have a corre...
Customizing Mock Behavior
• Invoke base class implementation if no expectation overrides the member
(a.k.a. "Partial Mocks...
Customizing Mock Behavior
• Make an automatic recursive mock: a mock that will return a new mock for
every member that doe...
Customizing Mock Behavior
• Centralizing mock instance creation and management: you can create and
verify all mocks in a s...
Miscellaneous
• Setting expectations for protected members (you can't get intellisense for
these, so you access them using...
Advanced Features
Advanced Features
• Mocking internal types of another project: add the following assembly
attribute (typically to the Asse...
Upcoming SlideShare
Loading in...5
×

Moq 的進階用法

254

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
254
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Moq 的進階用法"

  1. 1. Moq 的進階用法 Bryan Lin 2014/03/10
  2. 2. Agenda • Linq to Mocks • Methods • Matching Arguments • Properties • Events • Callbacks • Verification • Customizing Mock Behavior • Miscellaneous • Advanced Features
  3. 3. Linq to mocks • Moq is the one and only mocking framework that allows specifying mock behavior via declarative specification queries
  4. 4. Linq to mocks • Linq to Mocks is great for quickly stubbing out dependencies that typically don't need further verification. • If you do need to verify later some invocation on those mocks, you can easily retrieve them with Mock.Get(instance). • Traditional imperative mocking is also fully supported.
  5. 5. Methods • You can also mock objects by method call
  6. 6. Methods
  7. 7. Methods
  8. 8. Matching Arguments
  9. 9. Properties • Setup a property so that it will automatically start tracking its value (also known as Stub)
  10. 10. Properties • Stub all properties on a mock (not available on Silverlight): mock.SetupAllProperties();
  11. 11. Events
  12. 12. Callbacks
  13. 13. Callbacks
  14. 14. Verification
  15. 15. Verification
  16. 16. Customizing Mock Behavior • Make mock behave like a "true Mock", raising exceptions for anything that doesn't have a corresponding expectation: in Moq slang a "Strict" mock; default behavior is "Loose" mock, which never throws and returns default values or empty arrays, enumerables, etc. if no expectation is set for a member • var mock = new Mock(MockBehavior.Strict);
  17. 17. Customizing Mock Behavior • Invoke base class implementation if no expectation overrides the member (a.k.a. "Partial Mocks" in Rhino Mocks): default is false. (this is required if you are mocking Web/Html controls in System.Web!) • var mock = new Mock { CallBase = true };
  18. 18. Customizing Mock Behavior • Make an automatic recursive mock: a mock that will return a new mock for every member that doesn't have an expectation and whose return value can be mocked (i.e. it is not a value type)
  19. 19. Customizing Mock Behavior • Centralizing mock instance creation and management: you can create and verify all mocks in a single place by using a MockFactory, which allows setting the MockBehavior, its CallBase and DefaultValue consistently
  20. 20. Miscellaneous • Setting expectations for protected members (you can't get intellisense for these, so you access them using the member name as a string):
  21. 21. Advanced Features
  22. 22. Advanced Features • Mocking internal types of another project: add the following assembly attribute (typically to the AssemblyInfo.cs) to the project containing the internal types: • // This assembly is the default dynamic assembly generated Castle DynamicProxy, // used by Moq. Paste in a single line. [assembly:InternalsVisibleTo("DynamicProxyGenAssembly2,PublicKey=002400 0004800000940000000602000000240000525341310004000001000100c547cac3 7abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39 b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b19119 5b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861 eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×