mock

Mock non-virtual method giving compilation error

匿名 (未验证) 提交于 2019-12-03 02:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I need to write the gtest to test some existing code that has a non-virtual method, hence I am testing using the below source, but I am getting the compilation error sample_template_class3.cpp #include <iostream> #include <gtest/gtest.h> #include <gmock/gmock.h> using namespace std ; template < class myclass > class Templatemyclass { private : myclass T ; public : void display () { T . display (); } }; class Test { public : void display () { cout << "Inside the display Test:" << endl ; } }; class MockTest { public : MOCK_METHOD0 (

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

匿名 (未验证) 提交于 2019-12-03 02:26:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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 public void HubsGroupAreMockable() { var hub = new

Mocking the super class calls on python

匿名 (未验证) 提交于 2019-12-03 02:24:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am doing some unit testing and at some point I need to mock a super call to throw an error, for example: @classmethod def myfunc(cls, *args, **kwargs) try: super(MyClass, cls).my_function(args, kwargs) except MyException as e: #... I am using the mocker library to mock my objects in general but I haven't found a way to mock this. 回答1: With the mock library I would do something like this. In your class definition: from somelib import ASuperClass class MyClass(ASuperClass): def my_cool_method(self): return super(MyClass, self).my_cool_method

How to mock static methods in c# using MOQ framework?

匿名 (未验证) 提交于 2019-12-03 02:20:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have been doing unit testing recently and i successfully mocked various scenario with using MOQ framework and MS Tests by creating unit testing. As i know we can't do test private methods but using reflection but i want to know how can we test and mock unit tests by using MOQ framework. 回答1: Moq (and other DynamicProxy -based mocking frameworks) are unable to mock anything that is not a virtual or abstract method. Sealed/static classes/methods can only be faked with Profiler API based tools, like Typemock (commercial) or Microsoft Moles

Mocking HttpContextBase with Moq

匿名 (未验证) 提交于 2019-12-03 02:20:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a unit test fixture in which I'm trying to test a ControllerAction on an ASP.NET MVC controller that's used for membership functions on a web app. I'm trying to mock the HttpContext for the tests. The ControllerAction under test actually sets properties on the HttpContext, such as Session values, Response.Cookies values, etc. This isn't all of the code, but here is a rough sample of the test that I'm trying to get to run: [Test] public void ValidRegistrationDataSuccessfullyCreatesAndRegistersUser() { var context = new Mock ()

How to mock new Date() in java using Mockito

匿名 (未验证) 提交于 2019-12-03 02:18:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a function that uses the current time to make some calculations. I'd like to mock it using mockito. An example of the class I'd like to test: public class ClassToTest { public long getDoubleTime(){ return new Date().getTime()*2; } } I'd like something like: @Test public void testDoubleTime(){ mockDateSomeHow(Date.class).when(getTime()).return(30); assertEquals(60,new ClassToTest().getDoubleTime()); } Is it possible to mock that? I wouldn't like to change the "tested" code in order to be tested. 回答1: The right thing to do is to

Mocking boto3 S3 client method Python

匿名 (未验证) 提交于 2019-12-03 02:16:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to mock a singluar method from the boto3 s3 client object to throw and exception. But I need all other methods for this class to work as normal. This is so I can test a singular Exception test when and error occurs performing a upload_part_copy 1st Attempt import boto3 from mock import patch with patch('botocore.client.S3.upload_part_copy', side_effect=Exception('Error Uploading')) as mock: client = boto3.client('s3') # Should return actual result o = client.get_object(Bucket='my-bucket', Key='my-key') # Should return mocked

How to mock localStorage in JavaScript unit tests?

匿名 (未验证) 提交于 2019-12-03 02:16:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Are there any libraries out there to mock localStorage ? I've been using Sinon.JS for most of my other javascript mocking and have found it is really great. My initial testing shows that localStorage refuses to be assignable in firefox (sadface) so I'll probably need some sort of hack around this :/ My options as of now (as I see) are as follows: Create wrapping functions that all my code uses and mock those Create some sort of (might be complicated) state management (snapshot localStorage before test, in cleanup restore snapshot) for

Python mock patch doesn&#039;t work as expected for public method

匿名 (未验证) 提交于 2019-12-03 02:16:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to patch a public method for my flask application but it doesn't seem to work. Here's my code in mrss.feed_burner def get_feed(env=os.environ): return 'something' And this is how I use it @app.route("/feed") def feed(): mrss_feed = get_feed(env=os.environ) response = make_response(mrss_feed) response.headers["Content-Type"] = "application/xml" return response And this is my test which it's not parsing. def test_feed(self): with patch('mrss.feed_burner.get_feed', new=lambda: '<xml></xml>'): response = self.app.get('/feed') self

How to make mock to void methods with mockito

匿名 (未验证) 提交于 2019-12-03 02:10:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How to mock methods with void return type? I implemented an Observer pattern but I can't mock it with Mockito because I don't know how. And I tried to find an example on the Internet, but didn't succeed. My class looks like public class World { List<Listener> listeners; void addListener(Listener item) { listeners.add(item); } void doAction(Action goal,Object obj) { setState("i received"); goal.doAction(obj); setState("i finished"); } private string state; //setter getter state } public class WorldTest implements Listener { @Test public void