
Hello,
I wanted to mock the call to the HttpWebRequest service web in the unit tests using the XrmFakedContext; in this example I show you the code of the test function and the function that calls the web service Test Class:
[TestMethod, TestCategory("Unit example")]
public void test()
{
//Initialisation
fakeContext.Initialize(new List()
{
customer,
});
var inputs = new Dictionary { { "CustomerId", customer.ToEntityReference() }};
var result = fakeContext.ExecuteCodeActivity(inputs, null);
Assert.AreEqual(result["Exist"], true);
}private ReponseClass CallServiceWeb(string someParams)
{
try
{
var request = (HttpWebRequest)WebRequest.Create(someParams);
request.Method = "GET";
request.KeepAlive = true;
request.Accept = @"*/*";
var credential = new NetworkCredential(someParams, someParams, someParams);
var credentialCache = new CredentialCache();
credentialCache.Add(new Uri(url), "NTLM", credential);
request.Credentials = credentialCache;
string response = null;
using (var responseDetail = (HttpWebResponse)request.GetResponse())
{
using (var sr = new StreamReader(responseDetail.GetResponseStream()))
{
response = sr.ReadToEnd();
}
}
return this.Deserialize(response);
}
catch (Exception e)
{
return new ReponseClass () { erreurAppel = e };
}
}