stubbing a function using jest

With jest you should use jest.spyOn: jest .spyOn(jQuery, “ajax”) .mockImplementation(({ success }) => success([ 1, 2, 3 ])); Full example: const spy = jest.fn(); const payload = [1, 2, 3]; jest .spyOn(jQuery, “ajax”) .mockImplementation(({ success }) => success(payload)); jQuery.ajax({ url: “https://example.api”, success: data => spy(data) }); expect(spy).toHaveBeenCalledTimes(1); expect(spy).toHaveBeenCalledWith(payload); You can try live example on codesandbox: … Read more

Mocking using Moq in c#

Classic example which demonstrates that if you cannot unit test a particular component, REFACTOR it! This is why I love what any mocking framework enforces you to do – write decoupled code. In your example, the ProductBusiness class is tightly coupled with the ProductDataAccess class. You could decouple it using (like most of the answers … Read more

mockito verify interactions with ArgumentCaptor

I recommend using Mockito’s Hamcrest integration to write a good, clean matcher for it. That allows you to combine the verification with detailed checking of the passed argument: import static org.mockito.hamcrest.MockitoHamcrest.argThat; verify(mock, times(1)).someMethod(argThat(personNamed(“Bob”))); Matcher<Person> personNamed(final String name) { return new TypeSafeMatcher<Person>() { public boolean matchesSafely(Person item) { return name.equals(item.getName()); } public void describeTo(Description description) { … Read more

Mocking internal classes with Moq for unit testing

You could make internals visible to Moq by adding InternalsVisibleToAttribute in your project’s assembly.cs, like this: [assembly: InternalsVisibleTo(“DynamicProxyGenAssembly2”)] Why “DynamicProxyGenAssembly2” and not “Moq”? It’s the name of dynamic assembly created to contain dynamically generated proxy types (all of this is handled by yet another library, Castle’s DynamicProxy) which is used by Moq. So you expose … Read more

What are the advantages of Mocha over RSpec’s built in mocking framework? [closed]

Ruby mocking frameworks have evolved a lot since this question has been asked in 2009. So here is a little 2013 comparison: Expectations with Rspec-mocks: expect(user).to receive(:say_hello) with Mocha: user.expects(:say_hello).once Stubbing an object with Rspec-mocks: user = double(name: ‘John Doe’) with Mocha: user = stub(name: ‘John Doe’) Stubbing anything with Rspec-mocks: User.any_instance.stub(:name).and_return(‘John Doe’) with Mocha: … Read more

How can I mock a method in easymock that shall return one of its parameters?

Well, the easiest way would be to just use the Capture in the IAnswer implementation… when doing this inline you have to declare it final of course. MyService mock = createMock(MyService.class); final Capture<ParamAndReturnType> myCapture = new Capture<ParamAndReturnType>(); expect(mock.someMethod(capture(myCapture))).andAnswer( new IAnswer<ParamAndReturnType>() { @Override public ParamAndReturnType answer() throws Throwable { return myCapture.getValue(); } } ); replay(mock) This … Read more

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