Skip to content

Instantly share code, notes, and snippets.

@deejaygraham
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deejaygraham/7787aa578a33816c24e8 to your computer and use it in GitHub Desktop.
Save deejaygraham/7787aa578a33816c24e8 to your computer and use it in GitHub Desktop.
Integration testing of Azure targetted code without compute emulator
/// <summary>
/// Deployed implementation
/// </summary>
public class AzureRoleSettings : IRoleSettings
{
public string ReadValue(string key)
{
return RoleEnvironment.GetConfigurationSettingValue(key);
}
}
public interface IRoleSettings
{
string ReadValue(string key);
}
/// <summary>
/// Deployed implementation
/// </summary>
public class AzureRoleSettings : IRoleSettings
{
public string ReadValue(string key)
{
return RoleEnvironment.GetConfigurationSettingValue(key);
}
}
/// <summary>
/// Integration test version, reads from local cscfg
/// </summary>
public class LocalCloudConfigRoleSettings : IRoleSettings
{
private Dictionary<string, string> nameValuePairs = new Dictionary<string, string>();
public static LocalCloudConfigRoleSettings FromCloudConfig(string path, string roleName)
{
LocalCloudConfigRoleSettings settings = new LocalCloudConfigRoleSettings();
XElement doc = XElement.Load(path);
const string RoleElement = "Role";
const string NameAttribute = "name";
// namespace for cscfgs
XNamespace scns = "http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration";
// find the correct role
XElement requiredRole = (from role in doc.Descendants(scns + RoleElement)
where (string)role.Attribute(NameAttribute) == roleName
select role).FirstOrDefault();
if (requiredRole != null)
{
const string SettingElement = "Setting";
const string ValueAttribute = "value";
// import each name value pair
foreach (XElement setting in requiredRole.Descendants(scns + SettingElement))
{
XAttribute name = setting.Attributes(NameAttribute).FirstOrDefault();
XAttribute value = setting.Attributes(ValueAttribute).FirstOrDefault();
if (name != null && value != null)
{
settings.nameValuePairs.Add(name.Value, value.Value);
}
}
}
return settings;
}
public string ReadValue(string key)
{
return this.nameValuePairs[key];
}
}
/// <summary>
/// Unit test version (or mock IRoleSettings)
/// </summary>
public class StubRoleSettings : IRoleSettings
{
public Dictionary<string, string> nameValuePairs = new Dictionary<string, string>();
public string ReadValue(string key)
{
return this.nameValuePairs[key];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment