Mocking JavaScript constructor with Sinon.JS

You can either create a namespace or create a stub instance using sinon.createStubInstance (this will not invoke the constructor). Creating a namespace: const namespace = { Service: require(‘./service’) }; describe(‘Service’, function() { it(‘getData’, function() { sinon.stub(namespace, ‘Service’).returns(0); console.log(new namespace.Service()); // Service {} }); }); Creating a stub instance: let Service = require(‘./service’); describe(‘Service’, function() { … Read more

What is spec and spec_set

unittest.mock in Python 3.x is basically same with mock. According to the unittest.mock documentation: spec: This can be either a list of strings or an existing object (a class or instance) that acts as the specification for the mock object. If you pass in an object then a list of strings is formed by calling … Read more

Stubbing / mocking a database in .Net

From reading the other answers and various comments you’ve made, it seems you want an easier way to generate large populated datasets for integration testing that doesn’t hit the database. NBuilder is a great open-source library that I’ve successfully used to create large amounts of test data. Simply combine NBuilder, a few basic POCO object … Read more

Mocking a subprocess call in Python

It seems unusual to me that you use the patch decorator over the run_script function, since you don’t pass a mock argument there. How about this: from unittest import mock import subprocess def run_script(file_path): process = subprocess.Popen([“myscript”, -M, file_path], stdout=subprocess.PIPE) output, err = process.communicate() return process.returncode @mock.patch(“subprocess.Popen”) def test_run_script(self, mock_subproc_popen): process_mock = mock.Mock() attrs = … Read more

How to mock os.walk in python with a temporary filesystem?

No. os.walk() is constructed entirely around os.listdir(), with assistance of os.path.islink() and os.path.isdir(). These are essentially system calls, so you’d have to mock your filesystem at the system level. Unless you want to write a FUSE plugin this is not going to be easy to mock. All os.walk() needs to return is a list of … Read more

Python mock patch argument `new` vs `new_callable`

new is an actual object; new_callable is a callable used to create an object. The two cannot be used together (you either specify the replacement or a function to create the replacement; it’s an error to use both.) >>> foo = 6 >>> with mock.patch(‘__main__.foo’, new=7): … print foo … 7 >>> with mock.patch(‘__main__.foo’, new_callable=lambda … Read more

using mocker to patch with pytest

patch requires a path to the function being patched. You could do something like this: import pytest def sum(a, b): return a + b def test_sum1(mocker): mocker.patch(__name__ + “.sum”, return_value=9) assert sum(2, 3) == 9 def test_sum2(mocker): def crazy_sum(a, b): return b + b mocker.patch(__name__ + “.sum”, side_effect=crazy_sum) assert sum(2, 3) == 6 Result: $ … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)