gpt4 book ai didi

c# - SignalR:使用 moq 框架模拟 IHub 并测试他的方法

转载 作者:行者123 更新时间:2023-11-30 14:10:33 27 4
gpt4 key购买 nike

我尝试测试继承基类 Hub ( implements IHub interface ) 的自定义 RepositoryHub。我模拟了 IHub 的所有必要属性和我想测试的他的方法调用之一。方法调用被模仿为来自派生自定义类 RepositoyHub 的调用:
hubMock.Setup(p => p.OnConnected()).Returns(new RepositoryHub().OnConnected());
问题是该方法无法访问模拟的 IHub 字段。

    [TestMethod]
public void Is_OnConnected_Successive_When_Instances_is_Absent_And_User_Authenticated()
{
try
{
var connectionId = "1";

var request = new Mock<IRequest>();
request.Setup(s => s.User.Identity.Name).Returns(user + "&" + server + "&" + password + "&" + level);
request.Setup(s => s.User.Identity.IsAuthenticated).Returns(true);

var mockClients = new Mock<IHubCallerConnectionContext>();
var mockGroupManager = new Mock<IGroupManager>();
var mockHubCallerContext = new Mock<HubCallerContext>(request.Object, connectionId);

var hubMock = new Mock<IHub>();
hubMock.Setup(p => p.Groups).Returns(mockGroupManager.Object);
hubMock.Setup(p => p.Context).Returns(mockHubCallerContext.Object);
hubMock.Setup(p => p.Clients).Returns(mockClients.Object);
//Mock virtual method call
hubMock.Setup(p => p.OnConnected()).Returns(new RepositoryHub().OnConnected());


var r = hubMock.Object.OnConnected();
}
catch (Exception ex)
{
throw;
}
}
// Here method which i want to test with mocked IGroupManager, HubCallerContext and IHubCallerConnectionContext

public partial class RepositoryHub: Hub
{
public override Task OnConnected()
{
try
{
var userProfile = new UserProfile(Context.User);
//NullReference exception Context == null, but why i got it if mocked
//HubCallerContext in test method

Groups.Add(Context.ConnectionId, userProfile.ServerUrl);
// and other useful actions

return base.OnConnected();
}
catch (Exception ex)
{
Log.TraceError(String.Format(" Hub: OnConnected() {0}", ex.Message));
throw;
}
}
}

最佳答案


对于我来说,我找到了简单的方法。感谢关注!

            var repository = new RepositoryHub(){ Context = mockHubCallerContext.Object,
Clients = mockClients.Object,
Groups = mockGroupManager.Object };

var r = repository.OnConnected();

对于 JohnB,请回答您的最后一个问题:

[TestMethod]
public void Is_OnConnected_Successive()
{
const string connectionId = "1";
dynamic groupName = new ExpandoObject();

var IsSendMessageCalled = false;

groupName.sendMessage = new Action<object>((message) =>
{
IsSendMessageCalled = true;
Debug.WriteLine("sendMessage was called, message: {0}", message);
});

var request = new Mock<IRequest>();
request.Setup(s => s.User.Identity.Name).Returns(user + "&" + server + "&" + password + "&" + level);
request.Setup(s => s.User.Identity.IsAuthenticated).Returns(true);

var mockClients = new Mock<IHubCallerConnectionContext>();
mockClients.Setup(m => m.Group("groupName")).Returns((ExpandoObject)groupName);

var mockGroupManager = new Mock<IGroupManager>();
var mockHubCallerContext = new Mock<HubCallerContext>(request.Object, connectionId);

var hub = new RepositoryHub()
{
Context = mockHubCallerContext.Object,
Clients = mockClients.Object,
Groups = mockGroupManager.Object
};

var testMethod = hub.OnConnected();
Thread.Sleep(threadSleepTime);
//or testMethod.Wait();

Assert.IsTrue(IsSendMessageCalled);
}

让我们建议,在这里你调用你的 sendMessage(message) 方法:

public override Task OnConnected()
{
Hub.Clients.Group("groupName").sendMessage("Hello ! for groupName group ");
}

关于c# - SignalR:使用 moq 框架模拟 IHub 并测试他的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23475442/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com