Rspec: expect vs expect with block – what’s the difference?

As has been mentioned: expect(4).to eq(4) This is specifically testing the value that you’ve sent in as the parameter to the method. When you’re trying to test for raised errors when you do the same thing: expect(raise “fail!”).to raise_error Your argument is evaluated immediately and that exception will be thrown and your test will blow … Read more

NUnit Test Run Order

I just want to point out that while most of the responders assumed these were unit tests, the question did not specify that they were. nUnit is a great tool that can be used for a variety of testing situations. I can see appropriate reasons for wanting to control test order. In those situations I … Read more

Mocking methods on a Vue instance during TDD

Solution 1: jest.spyOn(Component.methods, ‘METHOD_NAME’) You could use jest.spyOn to mock the component method before mounting: import MyComponent from ‘@/components/MyComponent.vue’ describe(‘MyComponent’, () => { it(‘click does something’, async () => { const mockMethod = jest.spyOn(MyComponent.methods, ‘doSomething’) await shallowMount(MyComponent).find(‘button’).trigger(‘click’) expect(mockMethod).toHaveBeenCalled() }) }) Solution 2: Move methods into separate file that could be mocked The official recommendation is … Read more

C# – Asserting two objects are equal in unit tests

You’ve got two different Board instances, so your call to Assert.AreEqual will fail. Even if their entire contents appear to be the same, you’re comparing references, not the underlying values. You have to specify what makes two Board instances equal. You can do it in your test: Assert.AreEqual(expected.Rows.Count, actual.Rows.Count); Assert.AreEqual(expected.Rows[0].Cells[0], actual.Rows[0].Cells[0]); // Lots more tests … Read more

How do you mock the session object collection using Moq

I started with Scott Hanselman’s MVCMockHelper, added a small class and made the modifications shown below to allow the controller to use Session normally and the unit test to verify the values that were set by the controller. /// <summary> /// A Class to allow simulation of SessionObject /// </summary> public class MockHttpSession : HttpSessionStateBase … Read more

What do programmers mean when they say, “Code against an interface, not an object.”?

Consider: class MyClass { //Implementation public void Foo() {} } class SomethingYouWantToTest { public bool MyMethod(MyClass c) { //Code you want to test c.Foo(); } } Because MyMethod accepts only a MyClass, if you want to replace MyClass with a mock object in order to unit test, you can’t. Better is to use an interface: … Read more

tech