Share Point Development With Unit Testing


Published on

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Where – web parts, forms, event receivers, feature receivers, workflow, timer jobsApproaches – wrappers/facades, repositories, mocking
  • Spiking - Use PowerShell!Integration TestsWeb Tests
  • VMReflectorWSPBuilderVisual Studio 2008MOSS 2007 IUSQL 2008Create new WSPBuilder ProjectCreate new Feature ReceiverCreate new Unit Test ProjectAdd references: Nunit, SharePoint, Typemock, WSPBuilder ProjectAdd using statementsShow ReSharper, NunitAct | arrange | assert
  • Interfaces are rarely used: If our code that depends on an SPWeb could instead depend on an ISPWeb interface (assuming that SPWeb implemented ISPWeb), we could easily create a mock implementation of ISPWeb.  We could pass our mock into our code instead of an SPWeb. We could define the exact behavior of the mock. However, since SharePoint elements such as SPWeb, SPList, and SPItemEventProperties don’t implement public interfaces, this is not an option.Sealed classes: The SharePoint elements mentioned above are also sealed. Another mocking technique is to extend the type you want to mock overriding all the base class’s methods. Instances of the mock type can be substituted for the instances of the base type. However, since many SharePoint elements including the ones mentioned above are sealed, this technique is also not an option.Internal Constructor: SPWeb Even if there were no behavior in our dependent service that we need to override, we still would need to create an instance of it. Again, many SharePoint elements including the ones mentioned above have internal constructors so we are not expected to new up our instances, but rather get them for calling the SharePoint API.Collections with no Add method: Many of the collections provided by SharePoint such as SPItemEventDataCollection do not have a public Add method. This makes it difficult to mock if we can’t populate an instance of this collection with a controlled set of values. Pasted from <>
  • Demo FeatureReceiver1 thru FeatureReceiver4
  • Add try catch to check site not found, when passed in to see whether app code handles it
  • Wrapping everything with repository model
  • Another option is to provide an abstraction to the SharePoint API that can be mocked. A wrapper is an abstraction that provides a mirror of the SharePoint API. Pasted from <> A façade is an abstraction that simplifies the SharePoint API. Either approach takes a lot of work and may be very difficult to do well. Pasted from <>
  • Share Point Development With Unit Testing

    1. 1. SharePoint Development with Unit Testing<br />JEREMY THAKE<br />
    2. 2. OBJECTIVES<br />To explain the 3 goals of unit testing<br />To explain where you can unit test<br />To describe the 3 approaches to unit testing in SharePoint<br />
    3. 3. Unit Testing<br />Run quickly<br />Run on every developer machine<br />Minimal no config<br />(We are not testing MS code)<br />
    4. 4. Where to test?<br />ASP.NET Web Forms<br />Application Pages<br />Web Parts<br />Event Receivers<br />Feature Receivers<br />Workflow coding activities<br />Timer Jobs<br />
    5. 5. WHAT ITS NOT!<br />
    6. 6. UNIT TEST 101 SETUP<br />
    7. 7. TIGHTLY COUPLED CODE<br />
    8. 8. SharePoint + Mocking<br />Interfaces are rarely used<br />Sealed classes<br />Internal Constructors<br />TypeMockIsolator for SharePoint<br />
    9. 9. MOCKING 101<br />
    10. 10. NATURAL MOCKS<br />
    11. 11. SharePoint Guidance<br />
    12. 12. WRAPPERS & façades<br />
    13. 13. REPOSITORY<br />
    14. 14. MVP – Model View Presenter<br />
    15. 15. What to test?<br />TDD - lots of code when 80% is usually SharePoint code<br />Tests functionality and requirements work<br />Can cover scenarios and edge cases<br />Missing or empty URL variable<br />Valid URL variable syntax<br />Existence of the specified site<br />Missing or empty ListName variable<br />Existence of the specified list<br />Valid SPListItemCollection return object<br />Null or empty SPListItemCollection return object<br />
    17. 17. CONCLUSION<br />It not easy<br />“Lots of code”<br />Benefits<br />when refactoring<br />other developers changing it<br />environment dependencies reduced<br />speed<br />Doesn&apos;t stop poor quality code:<br />list.Items.Count<br />Dispose()<br />
    18. 18. JEREMY THAKE<br /><br />@jthake<br />