unit-testing

Mockito Spy calls the actual method of the spied class instead of returning the hardcoded mock

我只是一个虾纸丫 提交于 2021-02-07 20:01:24
问题 Here's my Spy statement OAuthService oAuthServiceMock = Mockito.mock(OAuthService.class); Mockito.doReturn(oAuthServiceMock).when(inviteServiceSpy.buildOAuthService(RESOURCE_URL, CONSUMER_KEY, CONSUMER_SECRET)); Here's the method in the actual class public OAuthService buildOAuthService(String RESOURCE_URL, String CONSUMER_KEY, String CONSUMER_SECRET) { return new ServiceBuilder() .provider(new DummyOAuth1ApiProvider(RESOURCE_URL)) .apiKey(CONSUMER_KEY) .apiSecret(CONSUMER_SECRET)

Form Regex that finds pattern within a repeating decimal

随声附和 提交于 2021-02-07 19:45:39
问题 How can I form a regular expression that match the unique numbers that repeat in a repeating decimals? Currently my regular expressions is the following. var re = /(?:[^\.]+\.\d*)(\d+)+(?:\1)$/; Example: // Pass deepEqual( func(1/111), [ "0.009009009009009009", "009" ] ); // Fails, since func(11/111) returns [ "0.099099099099099", "9" ] deepEqual( func(11/111), [ "0.099099099099099", "099" ] ); Live demo here: http://jsfiddle.net/9dGsw/ Here's my code. // Goal: Find the pattern within

Angular 4 - router.url unit testing

我与影子孤独终老i 提交于 2021-02-07 14:53:55
问题 How do I mock router.url in angular 4 unit testing? I use router.url in ngOnint in my component but in my test the value for router.url is '/' 回答1: you could use jasmine spyObj. In you TestBed : providers:[ { provide: Router, usevalue: jasmine.createSpyObj('Router',['methodOne','methodTwo]), }, ], in beforeEach: router = fixture.debugElement.injector.get(Router); in the test: it('should...', ()=> { (router.methodOne as Spy).and.returnValue(whateverValue)// if you wanna mock the response });

How can I unit test an event listener on an input field?

南楼画角 提交于 2021-02-07 14:30:35
问题 In the simplest test possible, I'm attempting to test the following function: addPercentSign: function (oEvent, control) { var inputVal = oEvent.getParameters().value; var inputNumber = parseFloat(inputVal); if (inputNumber) { if (inputNumber < 50 || inputNumber > 100) { //see learningCurveFormatCheck return null; } else { var finalVal = inputNumber.toFixed(1); var finalOutput = finalVal + "%"; control.learningCurve.setValue(finalOutput); return finalOutput; }; } } The above function is an

Should I create a new test method for each assertion?

烂漫一生 提交于 2021-02-07 14:30:21
问题 I know that this is subjective, but I'd like to follow the most common practice. Do you normally create one test method for each class method and stuff it with multiple assertions, or do you create one test method per assertion? For example, if I am testing a bank account's withdraw method, and I want make sure that an exception is thrown if the user tries to overdraw the account or withdraw a negative amount, should I create testOverdaw and testNegativeWithdrawal , or would I just combine

Mock 'window' object in Jasmine + Angular

▼魔方 西西 提交于 2021-02-07 13:16:38
问题 I have a function which I want to unit-test and into it I am comparing global objects window & parent as const isEqual = (window === parent) ; Which is the best way how to mock those objects in Angular/TypeScript? One more idea is to get those objects through function parameters, but anyway it's not solving this problem because I need to mock global window object too if I am using getSomeData(win: Window, parent: Window) { // ... } 回答1: I went with this solution: Create service which will

Mock 'window' object in Jasmine + Angular

安稳与你 提交于 2021-02-07 13:15:54
问题 I have a function which I want to unit-test and into it I am comparing global objects window & parent as const isEqual = (window === parent) ; Which is the best way how to mock those objects in Angular/TypeScript? One more idea is to get those objects through function parameters, but anyway it's not solving this problem because I need to mock global window object too if I am using getSomeData(win: Window, parent: Window) { // ... } 回答1: I went with this solution: Create service which will

In .NET Core 3.1, the RequestCookieCollection can no longer be used to create cookies in unit tests

我只是一个虾纸丫 提交于 2021-02-07 12:14:25
问题 I have just upgraded from .NET Core 2.2 to 3.1. I have tests to confirm that extension methods I've added to HttpContext.Request are working. I was previously able to do things like: var context = new DefaultHttpContext(); var c = new Dictionary<string, string> {{"test", "passed"}}; context.Request.Cookies = new RequestCookieCollection(cookies); var result = context.Request.GetPrimedValue(); Is this impossible now? I tried using Moq for this, but there are far too many things blocking me from

In .NET Core 3.1, the RequestCookieCollection can no longer be used to create cookies in unit tests

自作多情 提交于 2021-02-07 12:12:13
问题 I have just upgraded from .NET Core 2.2 to 3.1. I have tests to confirm that extension methods I've added to HttpContext.Request are working. I was previously able to do things like: var context = new DefaultHttpContext(); var c = new Dictionary<string, string> {{"test", "passed"}}; context.Request.Cookies = new RequestCookieCollection(cookies); var result = context.Request.GetPrimedValue(); Is this impossible now? I tried using Moq for this, but there are far too many things blocking me from

When to use a unit testing framework (vs. just using asserts)?

拟墨画扇 提交于 2021-02-07 12:11:33
问题 Using a small (currently at 150 loc, probably less than 500 when finished) C project I'm working on, I'm teaching myself test driven development. Based on some stuff I've found on the web - especially these slides by Olve Maudal, I've just been using asserts in my unit tests. Since I'm just learning tdd, I have thus far avoided the overhead of also learning a unit testing framework such as cunit. At this point, my thinking is that the additional learning curve - even if shallow - of a