moq

How do I add Group support to a mocked Client in a SignalR 2.x unit testing framework?

纵然是瞬间 提交于 2019-12-01 06:01:35
I'm using Moq to build up a UnitTest framework for my SignalR 2.x application. I am currently mocking up my Clients by: var mockClients = new Mock<IHubCallerConnectionContext>(); Clients = mockClients.Object; In order to test, I need to test sending messages by Group: Clients.Group(groupName).sendSomeMessage(message); How do I add Group support to my mocked up Client? Check this: https://github.com/SignalR/SignalR/blob/release/tests/Microsoft.AspNet.SignalR.Tests/Server/Hubs/HubFacts.cs public void HubsGroupAreMockable() { var hub = new MyTestableHub(); var mockClients = new Mock

Moq a base class function from a derived class

懵懂的女人 提交于 2019-12-01 04:53:25
I am new to Moq and I just watched the pluralsight video on Moqing so I felt empowered to go and write some tests. I have a Base Class let’s say Sheet which implements an interface ISheet. Sheet is the base class for pages: abstract class Sheet: ISheet { public virtual void CreateSheet() // Defined in ISheet { } public virtual void BuildSheet() // Defined in ISheet { } //and some abstract methods, etc. } public class Page : Sheet { public override void CreateSheet() { BuildSheet(); // Base class implementation } } I overrode one of the methods from the base class which is CreateSheet() , but I

Mock HttpClient using Moq

佐手、 提交于 2019-12-01 04:11:10
I like to unit test a class that use HttpClient . We injected the HttpClient object in the class constructor. public class ClassA : IClassA { private readonly HttpClient _httpClient; public ClassA(HttpClient httpClient) { _httpClient = httpClient; } public async Task<HttpResponseMessage> SendRequest(SomeObject someObject) { //Do some stuff var request = new HttpRequestMessage(HttpMethod.Post, "http://some-domain.in"); //Build the request var response = await _httpClient.SendAsync(request); return response; } } Now we like to unit test the ClassA.SendRequest method. We are using Ms Test for

How do I add Group support to a mocked Client in a SignalR 2.x unit testing framework?

扶醉桌前 提交于 2019-12-01 03:45:28
问题 I'm using Moq to build up a UnitTest framework for my SignalR 2.x application. I am currently mocking up my Clients by: var mockClients = new Mock<IHubCallerConnectionContext>(); Clients = mockClients.Object; In order to test, I need to test sending messages by Group: Clients.Group(groupName).sendSomeMessage(message); How do I add Group support to my mocked up Client? 回答1: Check this: https://github.com/SignalR/SignalR/blob/release/tests/Microsoft.AspNet.SignalR.Tests/Server/Hubs/HubFacts.cs

How to mock a method call that takes a dynamic object

杀马特。学长 韩版系。学妹 提交于 2019-12-01 03:23:27
Say I have the following: public interface ISession { T Get<T>(dynamic filter); } } And I have the following code that I want to test: var user1 = session.Get<User>(new {Name = "test 1"}); var user2 = session.Get<User>(new {Name = "test 2"}); How would I mock this call? Using Moq, I tired doing this: var sessionMock = new Mock<ISession>(); sessionMock.Setup(x => x.Get<User>(new {Name = "test 1")).Returns(new User{Id = 1}); sessionMock.Setup(x => x.Get<User>(new {Name = "test 1")).Returns(new User{Id = 2}); And that didn't work. The returned results is null I also tried to do the following with

Unit Test for a View Attribute using Moq

一世执手 提交于 2019-12-01 03:13:36
问题 I am using Moq for unit testing and I would like to test for a view's attribute. In this case the Authorize attribute. Example View Code: [Authorize(Roles = "UserAdmin")] public virtual ActionResult AddUser() { // view logic here return View(); } So I would like to test the view attribute when I act on this view with a user that is in the role of UserAdmin and a user that is not in the role of user admin. Is there anyway to do this ? Example Test: [Test] public void Index_IsInRole_Customer()

How do I raise an event when a method is called using Moq?

五迷三道 提交于 2019-12-01 03:05:57
I've got an interface like this: public interface IMyInterface { event EventHandler<bool> Triggered; void Trigger(); } And I've got a mocked object in my unit test like this: private Mock<IMyInterface> _mockedObject = new Mock<IMyInterface>(); I want to do something like this: // pseudo-code _mockedObject.Setup(i => i.Trigger()).Raise(i => i.Triggered += null, this, true); However it doesn't look like Raise is available on the ISetup interface that gets returned. How do I do this? Your pseudo-code was almost spot on. You needed to use Raises instead of Raise . Check the Moq Quickstart: Events

MOQ - verify exception was thrown

孤者浪人 提交于 2019-12-01 02:43:50
I working with MOQ framework for my testing. I have a scenario in which I expect a fault exception to be thrown. How can I verify it was thrown? public void Koko(List<string?> list) { foreach(string? str in list) { if (str != null) someProperty.Foo(str); else throw new FormatException(); } } Thanks in advance. treze If you want to verify an exception was thrown (by your own code) then Moq is not your tool of choice for that. Simply use one of the unit test frameworks available. Xunit/NUnit: Assert.Throws<SomeException>(() => foo.Bar()); Fluent Assertions: Action act = () => foo.Bar(); act

Mock IMemoryCache with Moq throwing exception

本小妞迷上赌 提交于 2019-12-01 02:28:21
I'm trying to mock IMemoryCache with Moq. I'm getting this error: An exception of type 'System.NotSupportedException' occurred in Moq.dll but was not handled in user code Additional information: Expression references a method that does not belong to the mocked object: x => x.Get<String>(It.IsAny<String>()) My mocking code: namespace Iag.Services.SupplierApiTests.Mocks { public static class MockMemoryCacheService { public static IMemoryCache GetMemoryCache() { Mock<IMemoryCache> mockMemoryCache = new Mock<IMemoryCache>(); mockMemoryCache.Setup(x => x.Get<string>(It.IsAny<string>())).Returns("")

Moq a base class function from a derived class

£可爱£侵袭症+ 提交于 2019-12-01 02:15:13
问题 I am new to Moq and I just watched the pluralsight video on Moqing so I felt empowered to go and write some tests. I have a Base Class let’s say Sheet which implements an interface ISheet. Sheet is the base class for pages: abstract class Sheet: ISheet { public virtual void CreateSheet() // Defined in ISheet { } public virtual void BuildSheet() // Defined in ISheet { } //and some abstract methods, etc. } public class Page : Sheet { public override void CreateSheet() { BuildSheet(); // Base