unit-testing

mock.patch and multiprocessing

Deadly 提交于 2021-01-29 08:20:22
问题 I'm struggling to use mock.patch in a multiprocessing environment while without multiprocessing mock.patch works fine. Filename: test_mp.py import multiprocessing import mock def inner(): return sub() def sub(): return "abc" def test_local(): assert inner()=="abc" def test_mp(): with multiprocessing.Pool() as pool: assert pool.apply(inner,args=[])=='abc' def test_mock(): with mock.patch('test_mp.sub', return_value='xxx') as xx: assert inner()=="xxx" xx.assert_called_once() def test_mp_mock():

I am mocking two functions exactly the same way. In one case the mock value is returned and in another case the real function is called. Why?

醉酒当歌 提交于 2021-01-29 08:14:15
问题 I have a file that exports some functions: function getNow() { console.log('real now'); return dayjs(); } function groupProducts(productInfos, now) { console.log('real group'); return productInfos.reduce((groups, productInfo) => { const groupKey = dayjs(productInfo.saleStartDate) > now ? dayjs(productInfo.saleStartDate).format('YYYY-MM-DD') : dayjs(now).format('YYYY-MM-DD'); let group = groups[groupKey]; if (!group) { group = []; // eslint-disable-next-line no-param-reassign groups[groupKey]

Angular 10 testing ViewContainerRef's injector returns undefined

∥☆過路亽.° 提交于 2021-01-29 08:11:18
问题 The problem happens after migrating our project from Angular 8.2.14 to Angular 10.2.24. This is the test code fdescribe('PopupModalService Testing', () => { let componentFactoryResolver: ComponentFactoryResolver; let viewContainerRef: ViewContainerRef; let popupModalService: PopupModalService; beforeEach(waitForAsync(() => { const viewContainerRefSpy = jasmine.createSpyObj('ViewContainerRef', ['insert']); TestBed.configureTestingModule({ declarations: [ PopupModalComponent, DialogApiComponent

What is the inverse of EXPECT_DEATH?

对着背影说爱祢 提交于 2021-01-29 07:54:32
问题 With google tests, let's assume the following code #include <iostream> using namespace std; using MyFunc = void (*)(void); void foo_robust(MyFunc f) { if(f != nullptr) (*f)(); } void foo_not_robust(MyFunc f) { (*f)(); } void print(void) { cout << "hello world" << endl; } int main() { foo(&print); //works foo(nullptr); //runtime error ? return 0; } When using google test, I can do: TEST(TestAssertDeath, Death) { EXPECT_DEATH(foo(&print)); //will return FAILED, because does not die. EXPECT

Mock function with jest that contains a callback as an argument which returns a promise

血红的双手。 提交于 2021-01-29 07:47:42
问题 I'm integrating with a 3rd party script and have come across a scenario which I don't know how to test properly. The 3rd party script adds an object to the window with a few methods. The second argument of the create method is a callback function which returns a promise that is either rejected or resolved. I need to be able to mock that second argument with either a reject or resolved status so that my unit test can cover whether the UI is updated with either the error or success message. How

Mock function with jest that contains a callback as an argument which returns a promise

别来无恙 提交于 2021-01-29 07:32:36
问题 I'm integrating with a 3rd party script and have come across a scenario which I don't know how to test properly. The 3rd party script adds an object to the window with a few methods. The second argument of the create method is a callback function which returns a promise that is either rejected or resolved. I need to be able to mock that second argument with either a reject or resolved status so that my unit test can cover whether the UI is updated with either the error or success message. How

Visual Studio 2019 suddenly requires nuget Microsoft.NET.Test.Sdk to run xUnit unit-tests

拜拜、爱过 提交于 2021-01-29 07:19:04
问题 I'm using xUnit for implementing tests. I used to install the xUnit Visual Studio runner, xunit.runner.visualstudio nuget package to run tests using the VS GUI. Below is are xUnit-related branches of the dependency tree in VS. You can clearly see the xUnit VS runner has no dependencies: Visual Studio is suddenly asking me to install the Microsoft.NET.Test.Sdk nuget in order to run tests in the GUI. Anyone knows why? Note: I have been using VS 2019 for over a year frequently updating asap. The

JUnit: mock a method called inside another method

馋奶兔 提交于 2021-01-29 07:11:54
问题 Consider I have a class Tournament with methods register() and isAlreadyRegistered() . Below is the sample code. public class Tournament { private boolean register(String teamName) { if(!isAlreadyRegistered(teamName)) { // register team return True; } return False; } private boolean isAlreadyRegistered(String teamName) { // Check if team is already registered, involves DB calls } public static void main(String[] args) throws Exception { Tournament tournament = new Tournament(); tournament

Using spy to mock a full object created in the class which is tested

最后都变了- 提交于 2021-01-29 07:01:14
问题 I have the following structure: class A { public A(String p){ // ... } public String AMethod(String p){ // ... } } class B { int method(String param){ A a = new A(param); int n; String s = A.AMethod(param); // ... (initializes n, ...) return n; } } Now I want to test method in class B but control the output of AMethod when it is called. But since I do not create the object A in the test class of B, I cannot mock it normally - how can I mock object A instead? I tried Mockito.spy but it doesn't

Verify call to File.Delete with System.IO.Abstractions.TestingHelpers

醉酒当歌 提交于 2021-01-29 06:52:15
问题 I use the System.IO.Abstractions.TestingHelpers to mock FileSystem. In my class, I inject IFileSystem and use the instance to call _fileSystem.File.Exists and _fileSystem.File.Delete. In my test class, I would like to verify that the "Delete" method was called. It's easy by mocking only the IFile, but since I already mocked the FileSystem, I don't want to have to mock the Directory, Path and File on top of it. Is it possible to call something like _fileRepository.FileMock.Verify(x => x.Delete