Posts

How To Mock Out Child Components In Unit Tests of Angular 2 Code

I was struggling with this one for a bit but found a solution this past weekend. When writing unit tests of a component that contains child components, the solution is to do the following: 1.       Create small mock classes in your test file to represent the child components and include just the functionality you’ll be mocking. These classes should have the @Component() decorator applied to them and contain selectors that match the real components they are being substituted for. 2.       Specify these mock components in the declarations section of the object you’re passing to TestBed.configureTestingModule(). Example: ActivityMgmtComponent has a few child components. Here are two of them: < activity-edit   #viewEditActivity ></ activity-edit > < activity-history   #viewActivityHistory ></ activity-history > Notice the # values – these are there to support the @ViewChild decorator f...

"Arithmetic operation resulted in an overflow" Error When Retrieving Data From Oracle In .NET

I ran into this fun little exception today when calling a stored procedure in Oracle from some .NET code. The error message was "Arithmetic operation resulted in an overflow", and from what I was able to Google, it seemed like it was caused by a difference in precision between numeric data types in .NET and Oracle, or  by the need to compile the code in 32-bit mode. But in my case, neither was the cause. When one of our DBAs suggested I try the same code using the simple "SELECT 1 FROM DUAL" query, and the exception still  occurred, I began to look for other reasons. Fortunately, it was an easy fix. The cause of the problem was actually a setting on the DevArt OracleCommand object being used in the .NET code. Here is the offending line of code: command.FetchSize =  int .MaxValue; Removing this line of code corrected the problem.

Using Webpack to Bundle CSS with Images and Fonts

One of my teams at work is currently developing in Angular 2, and we use Webpack for all of our TypeScript transpilation, module loading, and bundling. Yesterday we ran into an issue where one of the developers had made references to CSS files located in the node_modules directory of one of the 3rd party libraries we're using. This presented a problem because we don't distribute the node_modules folder as part of our build and deployment process -- we use JavaScript bundles created by Webpack. To correct the problem, I leveraged Webpack again, this time to bundle the required CSS files. The first step was to create a SASS file (.scss) which would be used as the entry point by Webpack. The reasoning behind using SASS was to allow us to do more than just reference the other stylesheets. But for the time being, it would do just that, and act as our entry point for Webpack to build its dependancy graph of our styles. Here's what the SASS file looked like at the start: @im...

Common Traits of The Best Developers I've Worked With

I've been fortunate to have worked with a lot of great developers over the years, and for all the differences in coding style, personality, and preferences that these diverse individuals have, they also happen to share several common traits. Below is a list of the qualities that I've found have been shared by all of the best developers I've worked with. They're collaborative -- not competitive These people work with  you, not in competition with you. They're team players, and want you to succeed as much as they want to succeed themselves. By contrast, whenever I hear a developer talk about keeping their "edge" over others, I know they're focused primarily on themself. That old saying about how there's "no I in team" is corny, but true. Will you be that cool dev that everyone feels they can come to with a problem, or the one who always has to sound like the smartest guy in the room? They value best practices Deisgn patterns, S.O.L.I.D. pri...

A Red Herring When Using Moq to Mock Methods With Optional Parameters

I recently spent some time writing unit tests for some code that required me to mock an object that has a method with optional properties. I used the Moq framework, and when I set up the method in question, I defaulted the optional parameters. This was required, as leaving them out of the Setup() call was not allowed. Also as part of my set up of this method, I instructed Moq to return the first parameter as the return value. I've had plenty of experience with Moq and setting up return values for mocked methods, but it didn't prepare me for the mistake I had unknowingly made. Here's how I set the method up originally: _repoMock.Setup(x =>  x.Save( It .IsAny< Click >(),  It .IsAny< string >(),  It .IsAny< bool >(),  It .IsAny< bool >()))     .Returns(( Click  click) => click); When I ran my unit test, the method above resulted in a "Parameter count mismatch" exception. I checked and doub...

How to Add a Reference to System.ServiceModel (WCF) to an ASP.NET 5 Application

I ran into a situation yesterday wherein I needed to add a reference to System.ServiceModel to an application using DNX RC1 Final Update 1 (if it's "final", why is there an update? I digress...). To do this, I had to add a frameworkAssemblies  property to the JSON object for dnx451  declared under frameworks  in my project.json file, and then add the System.ServiceModel reference there, as shown below:   "frameworks": {     "dnx451": {       "dependencies": {         "CMS.Services.Contracts": "1.0.0-*",         "CSharpSDK": "1.0.0-*"       },       "frameworkAssemblies": {         "System.ServiceModel": "4.0.0.0"       }     }   },

Using Underscore.js To Split an Array By Groups

Image
While working on a project today, I had to split an array of JSON objects into separate arrays based on one of the object's properties. Underscore.js makes this easy. In the example below, I'm splitting the sections  array into a new, multidimensional array called regions . var regions = _.toArray(_.groupBy(sections, function(section) {               return section.region;             })); Here's an example of the output logged to the browser's console: