2017-02-16 86 views
0

如何在ASP.net核心单元测试项目中模拟会话变量?如何在ASP.net核心单元测试项目中模拟会话变量?

1)我创建了一个会话的模拟对象。

Mock mockHttpContext = new Mock(); Mock mockHttpContext = new Mock(); Mock mockSession = new Mock()。As();

2)设置getString()方法

mockSession.Setup(S => s.GetString( “的moduleId”))返回( “1”)。

3)创建controllerContext和分配mockhttpContext对象

controller.ControllerContext.HttpContext = mockHttpContext.Object;

4)试图从控制器读取。

HttpContext.Session.GetString( “的moduleId”)

而我得到 “的moduleId” 的空值。请帮我嘲笑会议getString()方法

例子:

 //Arrange 
     //Note: Mock session 
     Mock<HttpContext> mockHttpContext = new Mock<HttpContext>(); 
     Mock<ITestSession> mockSession = new Mock<ISession>().As<ITestSession>(); 
     //Cast list to IEnumerable 
     IEnumerable<string> sessionKeys = new string[] { }; 
     //Convert to list. 
     List<string> listSessionKeys = sessionKeys.ToList(); 
     listSessionKeys.Add("ModuleId"); 
     sessionKeys = listSessionKeys; 
     mockSession.Setup(s => s.Keys).Returns(sessionKeys); 
     mockSession.Setup(s => s.Id).Returns("89eca97a-872a-4ba2-06fe-ba715c3f32be"); 
     mockSession.Setup(s => s.IsAvailable).Returns(true); 
     mockHttpContext.Setup(s => s.Session).Returns(mockSession.Object); 
    mockSession.Setup(s => s.GetString("ModuleId")).Returns("1");   

     //Mock TempData 
     var tempDataMock = new Mock<ITempDataDictionary>(); 
     //tempDataMock.Setup(s => s.Peek("ModuleId")).Returns("1"); 

     //Mock service 
     Mock<ITempServices> mockITempServices= new Mock<ITempServices>(); 
     mockITempServices.Setup(m => m.PostWebApiData(url)).Returns(Task.FromResult(response)); 

     //Mock Management class method 
     Mock<ITestManagement> mockITestManagement = new Mock<ITestManagement>(); 
     mockITestManagement .Setup(s => s.SetFollowUnfollow(url)).Returns(Task.FromResult(response)); 

     //Call Controller method 
     TestController controller = new TestController (mockITestManagement .Object, appSettings); 
     controller.ControllerContext.HttpContext = mockHttpContext.Object;    
     controller.TempData = tempDataMock.Object; 

     //Act 
     string response = await controller.Follow("true"); 

     // Assert 
     Assert.NotNull(response); 
     Assert.IsType<string>(response); 
+0

显示待测试的方法。 – Nkosi

+0

谢谢NKosi的回复。 –

+0

我得到了解决这个问题的方法。我在会话的模拟类上创建并从ISession继承。在这个模拟类中实现了所有的ISession方法,并使用这个类来存储会话变量。 –

回答

0

首先创建类命名mockHttpSession和继承的Isession。

公共类MockHttpSession:的Isession { 字典的sessionStorage =新词典();

public object this[string name] 
    { 
     get { return sessionStorage[name]; } 
     set { sessionStorage[name] = value; } 
    } 

    string ISession.Id 
    { 
     get 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    bool ISession.IsAvailable 
    { 
     get 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    IEnumerable<string> ISession.Keys 
    { 
     get { return sessionStorage.Keys; } 
    } 

    void ISession.Clear() 
    { 
     sessionStorage.Clear(); 
    } 

    Task ISession.CommitAsync() 
    { 
     throw new NotImplementedException(); 
    } 

    Task ISession.LoadAsync() 
    { 
     throw new NotImplementedException(); 
    } 

    void ISession.Remove(string key) 
    { 
     sessionStorage.Remove(key); 
    } 

    void ISession.Set(string key, byte[] value) 
    { 
     sessionStorage[key] = value; 
    } 

    bool ISession.TryGetValue(string key, out byte[] value) 
    { 
     if (sessionStorage[key] != null) 
     { 
      value = Encoding.ASCII.GetBytes(sessionStorage[key].ToString()); 
      return true; 
     } 
     else 
     { 
      value = null; 
      return false; 
     } 
    }   
**}** 

然后在实际控制人使用这个会话:

 Mock<HttpContext> mockHttpContext = new Mock<HttpContext>(); 
     MockHttpSession mockSession = new MockHttpSession();   
     mockSession["Key"] = Value; 
     mockHttpContext.Setup(s => s.Session).Returns(mockSession); 
     Controller controller=new Controller(); 
     controller.ControllerContext.HttpContext = mockHttpContext.Object; 
+0

我们可以使用这个会话类来存储会话变量。例如:首先创建mockHttpSession的对象,然后存储变量值。 –

0

我只是碰到了这个问题,最近进出的唯一方式是嘲笑的GetString方法包装,这是TryGetValue功能。

byte[] dummy = System.Text.Encoding.UTF8.GetBytes(Guid.NewGuid().ToString()); 
_mockSession.Setup(x => x.TryGetValue(It.IsAny<string>(),out dummy)).Returns(true).Verifiable(); 

因此,您不需要模拟对GetString方法的调用,只需模拟调用幕后的方法即可。

相关问题