Hier ist meine Controller Testklasse Methode, diezugewandt Problem während zu fälschen Methode in der Klasse Tring
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;
}
hier gefälschte Klasse initialisieren wird meine NMCSession Klasse
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; }
}
Nun ist die Frage an Controller des Benutzer ist zu versuchen, verwenden Sie die NMCSession.GetProducts()
, die mir Ausnahme gibt.
Nun, wie kann ich die oben genannten GetProducts()
Methode Anruf in NMCSession
vortäuschen?
Ich habe versucht, die NMCSession zu fälschen.Bitte sehen Sie den bearbeiteten Code.Aber ich bin mir nicht sicher, wie Sie Daten für GetProducts erhalten. Irgendein anderer Weg? Ich habe die 'GetProducts' Methodenimplementierung gesetzt. –
Sie scheinen nur die 'GetProducts' Methode in der Antwort wiederholt zu haben. Sie machen immer noch eine konkrete 'NMCSession' (in' fakeHttpSession.Contents ["NMCSession"] = new NMCSession() '), so dass die ursprüngliche Implementierung aufgerufen wird. Wenn du die 'NMCSession' fälschen willst, musst du 'A.Fake' machen. Und um 'GetProducts' mit einem eigenen Verhalten zu versehen, müssen' GetProducts' virtuell sein. –