locked
Unit Testing in Silverlight RRS feed

  • Question

  • I have created a Silverlight Project.  In my Silverlight Application I have a class called Car that gets values from the web.config file.

     

    public class Car
        {
            public Decimal Accelerate()
            {
               return decimal.Parse(HttpUtility.UrlDecode(App.DeploymentConfigurations["Acceleration"]));
            }
        }
    

    The start page for the application is Default.aspx.  The Default.aspx.cs file has the code that retrieves values from the web.config file.  Something like this.

      
    protected void Page_Load(object sender, EventArgs e)
            {
                SaveSilverlightDeploymentSettings(ParamInitParams);
            }
    
    
    
      private void SaveSilverlightDeploymentSettings(Literal litSettings)
            {
                NameValueCollection appSettings = ConfigurationManager.AppSettings;
    
                StringBuilder SB = new StringBuilder();
                SB.Append("<param name=\"InitParams\" value=\"");
    
                int SettingCount = appSettings.Count;
                for (int Idex = 0; Idex < SettingCount; Idex++)
                {
                    SB.Append(appSettings.GetKey(Idex));
                    SB.Append("=");
                    SB.Append(appSettings[Idex]);
                    SB.Append(",");
                }
                SB.Remove(SB.Length - 1, 1);
                SB.Append("\" />");
    
                litSettings.Text = SB.ToString();
            }
    
    
     
     The Accelerate() method in the Car class runs fine. 

    The problem is writing the unit test.  My unit test is in another Silverlight project.   And the unit test does not work.   I get a null reference exception on the following line of code.

     

    return decimal.Parse(HttpUtility.UrlDecode(App.DeploymentConfigurations["Acceleration"]));

     

    In my unit test, the Class Car object does not have access to the web config file.  How do I unit test the method Accelerate()

    In class Car?

      

    The following website talks about Silverlight applications accessing variables from a web config file.

     

    http://www.codeproject.com/KB/silverlight/SilverlightWebConfig.aspx
    Thursday, June 24, 2010 11:39 PM

Answers

  • In your scenario the page that runs the Silverlight unit test should be in the same project as the Silverlight code it is testing.

    So you could have the following projects

    MyProject (Silverlight)

    MyProjectTests (Silverlight Unit Tests,  Microsoft.Silverlight.Testing )

    MyWeb  ( Web application, containing launch page for your app AND launch page for your unit tests )

    Wednesday, June 30, 2010 11:07 AM