0

Here is my controller test class method which will initialize fake class

public void InitializeFake()
{
    fakeHttpSession = A.Fake<HttpSessionStateBase>();
    fakeHttpContext = A.Fake<HttpContextBase>();
    nmsFake = A.Fake<INMSPlatformClient>();

    isFakeInitialized = true;
    fakeHttpSession.Contents["NMCSession"] = new NMCSession()
    {
        DefaultOrganizationUID = 1,
        FirstName = "system",
        Login = "sys.admin",
        LastName = "Admin",
        IsMultiAccountViewable = true,
        OrganizationUID = 1,
        OrganizationName = "NMC",
    };


   var fake = (NMCSession)fakeHttpSession.Contents["NMCSession"];

    A.CallTo(() => fakeHttpContext.Session).Returns(fakeHttpSession);
    A.CallTo(() => fakeHttpSession["NMCSession"]).Returns(fake);
    A.CallTo(() => fakeHttpContext.Session).Returns(fakeHttpSession);
    ControllerContext = new ControllerContext(fakeHttpContext, new RouteData(), A.Fake<ControllerBase>());
    myController = new PlatformOrganizationController(nmsFake);
    myController.ControllerContext = ControllerContext;
}

here is my NMCSession class

public class NMCSession
{
    private List<Product> m_products=null;
    public string Login { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int OrganizationUID { get; set; }
    public string OrganizationName { get; set; }
    public string InstitutionCode { get; set; }
    public int DefaultOrganizationUID { get; set; }

    public string SessionToken { get; set; }

    public int UserUID { get; set; }

    public int NMCInactivityTimeOut { get; set; }

    public bool IsMultiAccountViewable { get; set; }

    public List<Product> GetProducts()
    {
       if(m_products!=null)
           return m_products;
       else
       {
           string NMSPlatformSvcUrl = System.Configuration.ConfigurationManager.AppSettings["NMSServerProtocol"] + "://" +
                                      System.Configuration.ConfigurationManager.AppSettings["NMSServerName"] + ":" +
                                      System.Configuration.ConfigurationManager.AppSettings["NMSServerPortNumber"];
            HttpClient httpClient = new HttpClient();
            httpClient.BaseAddress = new Uri(NMSPlatformSvcUrl);
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = httpClient.GetAsync("/NMS/Platform/ConfigurationSvc/v1/Status").Result;
            if (response.IsSuccessStatusCode)
            {
                PlatformStatus status = response.Content.ReadAsAsync<PlatformStatus>().Result;
                m_products=status.Products;
            }
            else
            {
                throw new Exception(response.StatusCode.ToString());
            }
           rn m_products;
       }
    }

    public bool IsDevelopmentPartner { get; set; }
}

Now the issue is at controller the user is to trying use the NMCSession.GetProducts() which is giving me exception.

Now how can I fake the above GetProducts() method call in NMCSession?

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
AMDI
  • 895
  • 2
  • 17
  • 40

1 Answers1

0

At minimum, you'd need to use a Fake NMCSession. In your test, you're instantiating a concrete NMCSession. Also note that unless GetProducts is virtual, FakeItEasy will not be able to intercept the calls, so the original implementation will be used.

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
  • I have tried to fake the NMCSession.Please see the edited code.But I am not sure how to get data for 'GetProducts'. Any other way ?I have put the 'GetProducts' Method implementation. – AMDI Aug 04 '16 at 12:55
  • You just seem to have repeated the `GetProducts` method in the answer. You're still making a concrete `NMCSession` (in `fakeHttpSession.Contents["NMCSession"] = new NMCSession()`), so the original implementation is called. If you want to fake the `NMCSession`, you'll have to make `A.Fake`. And in order to provide your own behaviour for `GetProducts`, `GetProducts` will have to be virtual. – Blair Conrad Aug 05 '16 at 10:35