Mocking open(file_name) in unit tests [duplicate]

This is admittedly an old question, hence some of the answers are outdated. In the current version of the mock library there is a convenience function designed for precisely this purpose. Here’s how it works: >>> from mock import mock_open >>> m = mock_open() >>> with patch(‘__main__.open’, m, create=True): … with open(‘foo’, ‘w’) as h: … Read more

Use Moq to mock Constructor?

var myMockBOC = new Mock<BusinessObjectContext>(null, null); This will pass nulls in for your two parameters. Another approach would be to create an internal constructor meant for test usage only, and use InternalsVisibleTo to allow your test assembly to use it. Unfortunately this has a big drawback in that if you sign your assemblies, Moq is … Read more

how to share a variable across modules for all tests in py.test

Update: pytest-namespace hook is deprecated/removed. Do not use. See #3735 for details. You mention the obvious and least magical option: using a fixture. You can apply it to entire modules using pytestmark = pytest.mark.usefixtures(‘big_dict’) in your module, but then it won’t be in your namespace so explicitly requesting it might be best. Alternatively you can … Read more

In Castle Windsor 3, override an existing component registration in a unit test

There are two things that you have to do to create an overriding instance: Assign it a unique name Call the IsDefault method So to get the example to work: this.WindsorContainer.Register( Component.For<IMediaPlayerProxyFactory>() .Instance(mockMediaPlayerProxyFactory) .IsDefault() .Named(“OverridingFactory”) ); Because I plan to use this overriding patten in many tests, I’ve created my own extension method: public static … Read more