Below is a Powershell script that will remove local Git branches except for a list of excluded branches.
Six Foot Coder
Connecting the developer community through conferences, user groups and training.
Friday, February 9, 2024
Cleanup Local Git Branches
Friday, December 29, 2023
Using MOQ
MOQ is a popular mocking framework for .NET used to create mock objects for testing. MOQ allows you to isolate the class under test by replacing its dependencies with controlled mock objects, making it easier to test behavior in isolation. Here are the steps to use it.
1. Install MOQ. Add MOQ using the NuGet package manager. https://www.nuget.org/packages/Moq
2. Create a Mock Object. Use Mock<T> where T is the type you want to mock. For example:
_webAPIClientMock = new Mock<IWebAPIClient>();
3, Setup Method Behavior. Define how the mock object should behave. For example:
_webAPIClientMock.Setup(client => client.GetApplicationSetting(It.IsAny<GetAppSettingRequest>()).Result).Returns(appSettingResponse);
4. Inject the mock. Inject the mock object into the class you are testing.
_apiSettingsProvider = new ApiSettingsProvider(Mock.Of<ILogger<ApiSettingsProvider>>(), _webAPIClientMock.Object);
5. Verify Interactions. Optionally, verify that certain interactions with the mock object occurred, like _webAPIClientMock.Verify(x => x.GetApplicationSetting(), Times.Once);
6. Run Your Test. Execute your test method as usual.
Full Example
public class ApiSettingsProviderTests
{
private Mock<IWebAPIClient> _webAPIClientMock;
private ApiSettingsProvider _apiSettingsProvider;
public void Initialize()
{
_webAPIClientMock = new Mock<IWebAPIClient>();
_apiSettingsProvider = new ApiSettingsProvider(Mock.Of<ILogger<ApiSettingsProvider>>(), _webAPIClientMock.Object);
}
[TestMethod]
public void GetAppSettingValue_ShouldReturnFirstValueFromAppSettingsResponse()
{
// Arrange
var appSettingName = "TestSetting";
var settingValues = new string[] { "Value1", "Value2" };
var appSettingResponse = new GetAppSettingResponse
{
SettingValues = new List<string>(settingValues)
};
_webAPIClientMock.Setup(client => client.GetApplicationSetting(It.IsAny<GetAppSettingRequest>()).Result).Returns(appSettingResponse);
// Act
var value = _apiSettingsProvider.GetAppSettingValue(appSettingName);
// Assert
Assert.AreEqual("Value1", value);
}
}
Resources
Creating Angular Tests and Mocking with Jasmine
When creating Angular Components, by default a spec.ts file is created. By definition, a unit test should not call any files, databases, or services. We use a mock, or a stand in for services.
Mocking Services
To mock services, the jasmine.createSpyObj is used. Example:
loggingService = jasmine.createSpyObj('LoggingService', ['Info','Error']);
Providing Mocked Services
To use the mocked service, it is provied. Example:
TestBed.configureTestingModule({
providers: [
ErrorHandlerService,
{ provide: LoggingService, useValue: loggingService },
{ provide: HttpTestingController, useValue: httpTestingController }
],
});
Full Example
We have an error handler service and we want to ensure that the message is logged by using the Error method.
import { HttpErrorResponse } from '@angular/common/http';
import { ErrorHandlerService } from './error-handler.service';
import { LoggingService } from './logging.service';
import { HttpTestingController } from '@angular/common/http/testing';
describe('ErrorHandlerService', () => {
let errorHandlerService: ErrorHandlerService;
let loggingService: LoggingService;
let httpTestingController: HttpTestingController;
beforeEach(() => {
loggingService = jasmine.createSpyObj('LoggingService', ['Info','Error']);
TestBed.configureTestingModule({
providers: [
ErrorHandlerService,
{ provide: LoggingService, useValue: loggingService },
{ provide: HttpTestingController, useValue: httpTestingController }
],
});
errorHandlerService = TestBed.inject(ErrorHandlerService);
httpTestingController = TestBed.inject(HttpTestingController);
});
it('should be created', () => {
expect(errorHandlerService).toBeTruthy();
});
it('should handle an error and log it', () => {
spyOn(console, 'error');
const error = new Error('Test error message');
errorHandlerService.handleError(error);
expect(console.error).toHaveBeenCalledWith(error);
expect(loggingService.Error).toHaveBeenCalledWith(error.message, [error]);
});
});
Thursday, October 5, 2023
Angular with .NET Core Powershell Setup Script
Here is a Powershell script that I made to automatically install everything that is needed to develop with Angular 16 and .NET Core 7.
Here's a breakdown:
1. Chocolatey Installation Check: At the beginning, the script checks if Chocolatey, a package manager for Windows, is already installed. If it is found at the specified location (`C:\ProgramData\chocolatey`), it prints a message indicating that Chocolatey is installed. If it is not found, the script adjusts the execution policy to allow the script to run, then it fetches and executes the installation script for Chocolatey from its official website.
2. API Software Installation: Through the Chocolatey package manager, the script proceeds to install the .NET 7.0 SDK, Visual Studio 2022 Community edition, web and data workloads for Visual Studio 2022, SQL Server Management Studio, and Postman. The flags `-y`, `--allow-empty-checksums`, and `--ignore-checksum` are used to automatically approve installations and bypass checksum verifications, which are useful in automated environments but can present security risks if the sources are not trusted.
3. UI Software Installation: For front-end or user interface development, the script installs Node.js LTS version and Visual Studio Code (VSCode) using Chocolatey.
4. VS Code Extensions Installation: Once VSCode is installed, the script installs a series of extensions for it. These extensions range from linters like ESLint, support for Angular development, CSS formatting tools, IntelliSense for paths and classes, and other utility extensions like auto-rename tags, icons, Prettier formatter, and GitHub's Copilot and Copilot Chat.
5. Angular CLI Installation: Finally, the script installs the Angular Command-Line Interface (CLI) globally using npm, the Node.js package manager. The Angular CLI is a vital tool for Angular developers, allowing them to create, manage, and deploy Angular applications with ease.
In summary, this script serves as a one-stop solution to set up a development environment with all the necessary tools and extensions for both API and UI development, primarily targeting .NET and Angular technologies.
Monday, October 2, 2023
Angular 16 Resources
There is a steep learning curve with Angular 16. Fortunately there are a lot of resources out there to ramp up.
Main Site
The main site for Angular is http://angular.io. It has documentation and a quick start which are semi-useful.
Free YouTube Courses
- A Complete Angular Course from Scratch: https://www.youtube.com/watch?v=3BIuwVnddG0&list=PL1BztTYDF-QNlGo5-g65Xj1mINHYk_FM9
- Angular for Beginners Course: https://www.youtube.com/watch?v=3qBXWUpoPHo
- Angular Crash Course: https://www.youtube.com/watch?v=3dHNOWTI7H8
Commercial Angular Course
https://www.udemy.com/course/real-world-app-angular-aspnet-core-web-api-and-sql/
Example Angular Project
- Angular UI: https://github.com/GregFinzer/CodePulse.UI
- .NET Core 7 Web API: https://github.com/GregFinzer/CodePulse.API
Quick References
- https://cheatography.com/gregfinzer/cheat-sheets/angular/
- https://cheatography.com/rajanvora/cheat-sheets/angular-cheat-sheet/
- https://cheatography.com/spegusess/cheat-sheets/angular/
- https://cheatography.com/gregfinzer/cheat-sheets/angular-cli/
- https://cheatography.com/wakers01/cheat-sheets/angular-cli/
Saturday, September 30, 2023
Software Development Requirements for Angular
Install the LTS Version of Node
https://nodejs.org
Or install with Chocolatey
choco install nodejs-lts
Install the Angular CLI
At the command prompt
npm install -g @angular/cli
Install VS Code
https://code.visualstudio.com/
Or install with Chocolatey
choco install vscode
Install VS Code Extensions
ESLint: https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint
Angular Snippets (Version 16): https://marketplace.visualstudio.com/items?itemName=johnpapa.Angular2
CSS Formatter: https://marketplace.visualstudio.com/items?itemName=aeschli.vscode-css-formatter
Angular Language Service: https://marketplace.visualstudio.com/items?itemName=Angular.ng-template
IntelliSense for CSS class names in HTML: https://marketplace.visualstudio.com/items?itemName=Zignd.html-css-class-completion
Path Intellisense: https://marketplace.visualstudio.com/items?itemName=christian-kohler.path-intellisense
Or install on the command line:
"C:\Program Files\Microsoft VS Code\bin\code" --install-extension dbaeumer.vscode-eslint
"C:\Program Files\Microsoft VS Code\bin\code" --install-extension johnpapa.Angular2
"C:\Program Files\Microsoft VS Code\bin\code" --install-extension aeschli.vscode-css-formatter
"C:\Program Files\Microsoft VS Code\bin\code" --install-extension Angular.ng-template
"C:\Program Files\Microsoft VS Code\bin\code" --install-extension Zignd.html-css-class-completion
"C:\Program Files\Microsoft VS Code\bin\code" --install-extension christian-kohler.path-intellisense
Friday, September 29, 2023
Developer Software Requirements for Creating a .NET Core Web API
Install .NET 7 SDK
https://dotnet.microsoft.com/en-us/download
Or Install with Chocolatey
choco install dotnet-7.0-sdk
Install Visual Studio 2022
https://visualstudio.microsoft.com
Choose workloads
ASP.NET and Web Development
Data Storage and Processing
Or Install One of These with Chocolatey
choco install visualstudio2022community
choco install visualstudio2022professional
choco install visualstudio2022enterprise
Then install workloads
choco install visualstudio2022-workload-netweb
choco install visualstudio2022-workload-data
Install SQL Server Developer Edition
https://www.microsoft.com/en-us/sql-server/sql-server-downloads
Choose Basic
Install SQL Server Management Studio or Database .NET
SQL Server Management Studio
Or install with Chocolatey
choco install sql-server-management-studio
Database.NET
https://fishcodelib.com/database.htm
Or install with Chocolatey
choco install databasenet
Install Postman
https://www.postman.com/downloads/
Or install with Chocolatey
choco install postman