Reasons for using mocking objects

Reasons for use

In a unit test, mock objects can simulate the behavior of complex, real (non-mock) objects and are therefore useful when a real object is impractical or impossible to incorporate into a unit test. If an object has any of the following characteristics, it may be useful to use a mock object in its place:

  • supplies non-deterministic results (e.g. the current time or the current temperature);
  • has states that are difficult to create or reproduce (e.g. a network error);
  • is slow (e.g. a complete database, which would have to be initialized before the test);
  • does not yet exist or may change behavior;
  • would have to include information and methods exclusively for testing purposes (and not for its actual task).

For example, an alarm clock program which causes a bell to ring at a certain time might get the current time from the outside world. To test this, the test must wait until the alarm time to know whether it has rung the bell correctly. If a mock object is used in place of the real object, it can be programmed to provide the bell-ringing time (whether it is actually that time or not) so that the alarm clock program can be tested in isolation.

LINQ “Any” Keyword to check for existence of an element/attribute

One of the things you must do when writing a LINQ to Xml query definition is check for the existence of nonrequired elements and attributes before using them. For instance, a client application displays an image of the book in the ListBox control. The source URL of the image is grabbed from the SmallImage element’s URL element. If the SmallImage element does not exist, which is certainly a possibility if there is no image on record, the LINQ query definition must account for this.

For example,

<Item>
    <SmallImage>
        <URL>



        </URL>
    </SmallImage>
</Item>

 

Before grabbing a value, you can perform a simple check to make sure the element exists; you do this by checking the Elements collection’s Any method. The Any method returns a Boolean value that indicates whether the collection contains elements. If the element exists, you can grab the value of the element. If the element does not exist you can set alternatives. The following shows the logic that checks for the existence of the image’s URL; if it is found it grabs the value and sets it to the imageSource variable.

let imageSource = (book.Elements(ns + "SmallImage").Any()
    ? book.Element(ns + "SmallImage").Element(ns + "URL").Value
    : string.Empty)
p.s. for more on the usage of the keyword let, take a look at http://vincenthomedev.wordpress.com/2007/12/19/linq-let-keyword/
 

How to design a Fluent Interface (Method Chaining)

 

Martin Fowler coined the term "FluentInterface" to describe objects that expose an interface that flows, and is designed to be readable and concise.  The cost of this fluency is additional effort required to design the interface for your object and the slight increase in complexity.  These types of interfaces are often utilized to create configurations for your objects but can progress into an internal Domain Specific Language or DSL.

  1. Person person = new Person();  
  2. person.Set.FirstName("Pat").LastName("Frank").Age(30).IsActive();  

Create an internal class that is accessed through a Set property and exposes only the Fluent Interface.

  1. public class Person  
  2. {  
  3.          public Person() {  _set = new PersonFluentInterface(this);               }  
  4. private string _firstName;  
  5. private string _LastName;  
  6. private int _age;  
  7. private readonly PersonFluentInterface _set;  
  8. private bool _isActive;  
  9. public PersonFluentInterface Set   {   get { return _set; }               }  
  10. public string FirstName      {   get { return _firstName; }   set { _firstName = value; }               }  
  11. public string LastName    {   get { return _LastName; }   set { _LastName = value; }               }  
  12. public int Age   {   get { return _age; }   set { _age = value; }               }  
  13. public bool IsActive  {   get { return _isActive; }   set { _isActive = value; }               }  
  14.           public class PersonFluentInterface  
  15.           {  
  16. private readonly Person _person;  
  17. public PersonFluentInterface(Person person) {      _person = person;                   }  
  18. public PersonFluentInterface FirstName(string firstName)   {     _person.FirstName = firstName;   return this;                   }  
  19. public PersonFluentInterface LastName(string lastName)    {     _person.LastName = lastName;   return this;                   }  
  20. public PersonFluentInterface Age(int age)    {     _person.Age = age;   return this;                   }  
  21. public PersonFluentInterface IsActive()   {   _person.IsActive = true;   return this;                   }  
  22. public PersonFluentInterface IsNotActive()    {    _person.IsActive = false;   return this;                   }  
  23. }           } 

Code to Live, Live to Code – How to design a Fluent Interface

Jesse Liberty – Creating Silverlight Carousel Videos

 image

Jesse Liberty videos Creating a Carousel Part 1 and Creating a Carousel Part 2 are now available.  These videos cover the material that is also discussed in his two part blog entry (beginning here) including such advanced topics as

  • The Silverlight Layout System
  • Overriding MeasureOverride and ArrangeOverride
  • Matrix Transforms
  • Adding Attached Dependency Properties
  • Programmatic Animation

nerdplusart.com | Silverlight Code Snippets for DependencyProperties and other Handy Stuff

 

Download The Snippets

If you’re new to VS snippets, you can get the skinny here.  To use them you just install the .vsi (see link above) and then type the snippet shortcut (e.g. sldp), hit tab a couple of times to kick off the snippet and then start providing the values it asks for (hit tab to move between values).  When you’re done, hit enter.

The Short Descriptions

Here’s the short guide to the snippets.  A little more detail follows.

sldp
A basic Silverlight DependencyProperty

sldpc
A Silverlight DependencyProperty with a property changed callback

sldpa
An attached Silverlight Dependency Property

slevent
A really basic event definition

slsbc
An inline, anonymous Storyboard.Completed handler (check this one out, it’s cooler than it sounds)

slpanel
A really simple implementation of a panel

slrss
A super basic collection class that loads an RSS feed

slvc
The boilerplate code you need to create a value converter

Examples: nerdplusart.com | Silverlight Code Snippets for DependencyProperties and other Handy Stuff

Prism 2 – Composite Application Guidance for WPF and Silverlight

 

image image

This version of the Composite Application Guidance is designed to help you build applications in WPF and Silverlight that have a single code base. The guidance uses a number of design patterns. Familiarity with these technologies and patterns is useful for evaluating and adopting the Composite Application Library.

Downloads Composite Application Guidance for WPF and Silverlight

Getting Started

Evaluating the Composition Application Guidance

Overview

Stock Trader Reference Implementation

QuickStarts

WPF Hands-On Lab: Getting Started with the Composite Application Library

Silverlight Hands-On Lab: Getting Started with the Composite Application Library

Upgrading from the Composite Application Guidance for WPF-June 2008

Community Feedback and Support

CodePlex

 

Included in the Composite Application Guidance

Stock Trader Reference Implementation (Stock Trader RI)

This is a sample composite application that is based on a real-world scenario. This intentionally incomplete application illustrates the Composite Application baseline architecture. This is a good reference to see how many of the challenges are addressed by this guidance when building composite applications.

Composite Application Library source code

Developers can use the Composite Application Library to develop WPF or Silverlight applications that are composed of independent and collaborating modules. The library includes extensions to support the integration of the Unity Application Block.

QuickStarts

These include the source code for several small, focused applications that illustrate user interface (UI) composition, modularity, commanding, event aggregation, and multi-targeting applications between WPF and Silverlight. The Getting Started Hands-On Labsprovide step-by-step instructions to create your first application using the Composite Application Library in WPF or Silverlight.

Documentation

This includes the architectural overview, Stock Trader RI overview, design and technical concepts for composite applications, applied patterns, How-to topics, QuickStart overviews, and deployment topics. Much of this guidance is applicable even if you are not using the Composite Application Library, but you want to know best practices for creating composite applications.

 

Via Composite Application Guidance for WPF and Silverlight

Silverlight 2 & 3 Unit Test Frameworks

1. Microsoft Silverlight Unit Test Framework

The source code to the unit test framework is now available on CodePlex: the Silverlight Toolkit
download new templates
breaking changes information
The unit test framework is compatible with Microsoft Silverlight 2. Windows and Mac. Safari, Mozilla Firefox, and Internet Explorer support.
simple. powerful. extensible.

  • unit tests run right inside the web browser
  • enables testing of rich controls and the entire Silverlight platform
  • rich in-browser logging
  • basic asynchronous testing support

sample application screenshot

Also

  • ScottGu introduction
  • Introductory post by Jeff Wilcox
  • Introductory video and screencast
  • Visual Studio Unit Test Intro
  • 2. SilverUnit – Roy Osherove (VB.NET)

    3. Selenium

    Indeed’s New Industry Trends Point To Where The Jobs Are

     

    Tech layoffs may have hit 300,000 since the financial crisis began, but there are at least 395,629 job openings in information technology, enough to re-employ all of those now out of work. Job search engine Indeed this morning launched a new Industry Trends page filled with stats on job openings in the U.S. across major industries. Although there are more job openings in IT than in any other industry except healthcare (which has 581,625 job listings).

     

    Via Indeed’s New Industry Trends Point To Where The Jobs Are

    Silverlight Layout System (SLS) – Jesse Liberty

    Part I

    The first thing to know about the SLS is that most of the time you can ignore it! It is possible to become quite proficient in Silverlight programming without even knowing explicitly that the system exists, much less having to override any of its methods. For most developers, most of the time, the layout system is implicit and mediated for you by layout controls such as the GridPanel and StackPanel, and more recently by the Silverlight Toolkit layout controls such as the DockPanel and the WrapPanel.

    That said, there are times when you want to do something the existing controls just don’t provide, and familiarity with how Silverlight lays out controls can be both fascinating and essential.

    via The Layout Model – Jesse Liberty – Silverlight Geek

    Part II  Putting the layout system to work