mocking

Are mocks better than stubs?

我是研究僧i 提交于 2021-02-18 04:56:52
问题 A while ago I read the Mocks Aren't Stubs article by Martin Fowler and I must admit I'm a bit scared of external dependencies with regards to added complexity so I would like to ask: What is the best method to use when unit testing? Is it better to always use a mock framework to automatically mock the dependencies of the method being tested, or would you prefer to use simpler mechanisms like for instance test stubs? 回答1: As the mantra goes 'Go with the simplest thing that can possibly work.'

Are mocks better than stubs?

房东的猫 提交于 2021-02-18 04:56:17
问题 A while ago I read the Mocks Aren't Stubs article by Martin Fowler and I must admit I'm a bit scared of external dependencies with regards to added complexity so I would like to ask: What is the best method to use when unit testing? Is it better to always use a mock framework to automatically mock the dependencies of the method being tested, or would you prefer to use simpler mechanisms like for instance test stubs? 回答1: As the mantra goes 'Go with the simplest thing that can possibly work.'

Find method not working with EF6.1 mock

半城伤御伤魂 提交于 2021-02-17 03:59:07
问题 I've setup mocking using these msdn guidlelines: Testing with a mocking framework (EF6 onwards) var bsAc = _db.BusAcnts.FirstOrDefault(i => i.Id == 1); returns an account but var bsAc = _db.BusAcnts.Find(1); returns null when mocked. Find only fails when testing with a mock, it works fine in production. BusAcnt: (Id is the Primary Key) public class BusAcnt { public int Id { get; set; } ... } See the rest of my setup here. In debug I drilled down into Locals | this | MyDbContext and all the

Find method not working with EF6.1 mock

二次信任 提交于 2021-02-17 03:54:51
问题 I've setup mocking using these msdn guidlelines: Testing with a mocking framework (EF6 onwards) var bsAc = _db.BusAcnts.FirstOrDefault(i => i.Id == 1); returns an account but var bsAc = _db.BusAcnts.Find(1); returns null when mocked. Find only fails when testing with a mock, it works fine in production. BusAcnt: (Id is the Primary Key) public class BusAcnt { public int Id { get; set; } ... } See the rest of my setup here. In debug I drilled down into Locals | this | MyDbContext and all the

Check for null values in request body using Wiremock

血红的双手。 提交于 2021-02-17 02:04:52
问题 I am trying to setup a wiremock stub that will return a 400 error if any field has a null value in the json payload. Basically to simulate a Bad Request. I've been trying with a regex that matches any lowercase string for the json key but it doesn't seem to like it. I can't find any examples of what I want online so not sure if it's even possible. My Bad Request body: { "cat": null, "dog": { "id": 1344 }, "horse": { "id": 1 }, "fish": 1 } My Stub: wireMockServer.stubFor(post(urlEqualTo("

How to override a method in unit tests that is called from which the class being tested

折月煮酒 提交于 2021-02-16 18:34:13
问题 I am testing a class A's function func1. Func1 has a local variable of Class B and calls B's function func2. Code looks something like this: public Class A { public func1() { B object = new B(); int x = object.func2(something); } } When I am testing func1 in its unit tests, I don't want func2 to get called. So I am trying to do something like this in the test: B textObject = new B() { @override int func2(something) { return 5; } } But it is still calling the func2 in the class B. Please

How to override a method in unit tests that is called from which the class being tested

百般思念 提交于 2021-02-16 18:33:06
问题 I am testing a class A's function func1. Func1 has a local variable of Class B and calls B's function func2. Code looks something like this: public Class A { public func1() { B object = new B(); int x = object.func2(something); } } When I am testing func1 in its unit tests, I don't want func2 to get called. So I am trying to do something like this in the test: B textObject = new B() { @override int func2(something) { return 5; } } But it is still calling the func2 in the class B. Please

Mocked unit test raises a “stop called on unstarted patcher” error

谁说胖子不能爱 提交于 2021-02-16 14:45:38
问题 When running the test bellow, I got a stop called on unstarted patcher . def test_get_subvention_internal_no_triggered_admission(self): billing_cluster = BillingClusterFactory() subvention = SubventionFactory(billing_cluster=billing_cluster) convive_sub = ConviveFactory(subvention=subvention, billing_cluster=billing_cluster) order_5 = OrderFactory(beneficiary=convive_sub) order_operation_5 = CreationOrderOperationFactory(order=order_5) with patch('orders.models.Order.subvention_triggered_same

Python unittest patch mock entier class

依然范特西╮ 提交于 2021-02-11 13:14:33
问题 I have a class that I want to patch in my unittests. class OriginalClass(): def method_a(): # do something def method_b(): # do another thing Now I created another class to patch it with, so the code for patching it is like class MockClass(OriginalClass): def method_a(): # This will override the original method and return custom response for testing. patcher = patch('OriginalClass', new=MockClass) mock_instance = patcher.start() This works exactly as I want it to and I can return whatever

Python unittest patch mock entier class

邮差的信 提交于 2021-02-11 13:11:42
问题 I have a class that I want to patch in my unittests. class OriginalClass(): def method_a(): # do something def method_b(): # do another thing Now I created another class to patch it with, so the code for patching it is like class MockClass(OriginalClass): def method_a(): # This will override the original method and return custom response for testing. patcher = patch('OriginalClass', new=MockClass) mock_instance = patcher.start() This works exactly as I want it to and I can return whatever