Wednesday, November 15, 2017

Chrome browser popular keyboard shortcut keys cheat sheet

We all use chrome browser so often. 60% of internet users around world use chrome browser. It is first choice of any sensible guy.

Never a single day passes out with a chrome browser for a professional guy. It makes sense to know some important keyboard shortcuts for chrome which can improve all our lives and save us some time. Let's all be Chrome smart folks !

CHROME SHORTCUT KEYS

OPEN CHROME MENU (Burger)

ALT + F

OPEN DOWNLOADS MENU

CTRL + J

OPEN HISTORY MENU

CTRL + H

OPEN DEV TOOLS

CTRL + SHIFT + J

OPEN CLEAR BROWSER DATA

CTRL + SHIFT + DEL

OPEN NEW WINDOW

CTRL+N

OPEN NEW WINDOW (INCOGNITO)

CTRL + SHIFT + N

OPEN NEW TAB

CTRL + T

REOPEN CLOSED TAB

CTRL + SHIFT +T

BOOKMARK

CTRL + D

BOOKMARK MANAGER

CTRL + SHIFT +O

TOGGLE BOOKMARK BAR

CTRL + SHIFT + B

CLOSE WINDOW

ALT + F4

CLOSE TAB

CTRL +F4

CLOSE CHROME

CTRL + SHIFT + Q

ADDRESS BAR FOCUS

CTRL + K, ALT + D

MOVE BETWEEN TAB

CTRL +TAB

RELOAD PAGE (IGNORING CACHE)

SHIFT + F5

DENY NOTIFICATION

ALT + SHIFT + A

BACK AND FORTH BROWSING HISTORY

ALT + -> OR ALT + <- o:p="">


.Net Core Web Api CRUD Controller unit testing code sample

Unit testing in .Net Core has been made simple. It has been designed with testability in mind. TDD approach has paid rich dividends for the developers who follow them religiously. Unit tests gives you the confidence and faith in your code, catches errors early in the cycle and overall, improves productivity levels across the team. There is a upfront cost with unit testing but, it's worth the cost and effort. This code sample unit tests a typical CRUD web api controller. This code sample is compiled on .Net Core 2.0 and .Net Core Standard 2.0. We used xUnit as testing framework and test runner, Moq for mocking dependencies.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using AutoMapper;
using Moq;
using VisualStudio99.Data.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using VisualStudio99.Api.Controllers;
using Xunit;
using VisualStudio99.Common.Dto;
using VisualStudio99.Tests.Data;
using VisualStudio99.Data;
using VisualStudio99.Data.Mappers;

namespace VisualStudio99.Tests
{
    public class EmployeeControllerTests
    {
        private readonly Mock<IEmployeeRepository> mockRepo;
        private readonly Mock<EmployeeMapper> mockMapper;
        private readonly EmployeeController controller;
        public EmployeeControllerTests()
        {
            mockRepo = new Mock<IEmployeeRepository>();
            mockMapper = new Mock<EmployeeMapper>();
            controller = new EmployeeController(mockRepo.Object, mockMapper.Object);
        }
        [Fact]
        public async Task Post_ReturnsOkResult_WhenCreatingValidEmployee()
        {
            // Arrange & Act

            mockRepo.Setup(repo => repo.Add(It.IsAny<Employee>())).Verifiable();
            mockRepo.Setup(repo => repo.SaveAsync()).ReturnsAsync(1);
            mockRepo.Setup(repo => repo.AnyAsync(It.IsAny<Expression<Func<Employee, bool>>>())).Returns(Task.FromResult(false));
            // Act
            var result = await controller.Post(EmployeeData.GetNewEmployee());

            //Assert
            mockRepo.Verify(repo => repo.Add(It.IsAny<Employee>()), Times.Once);
            mockRepo.Verify(repo => repo.SaveAsync(), Times.Once);
            var createdAtRouteResult = Assert.IsType<CreatedAtActionResult>(result);
            var employee = Assert.IsType<EmployeeDto>(createdAtRouteResult.Value);
            Assert.Equal("TestEmployee", employee.Name);
        }

        [Fact]
        public async Task Post_ReturnsBadRequest_WhenUpdatingInvalidNullEmployee()
        {

            // Act
            var result = await controller.Post(null);

            //Assert
            mockRepo.Verify(repo => repo.Add(It.IsAny<Employee>()), Times.Never);
            Assert.IsType<BadRequestObjectResult>(result);
        }
        [Fact]
        public async Task Post_ReturnsBadRequest_WhenAddingExistingEmployee()
        {


            mockRepo.Setup(repo => repo.AnyAsync(It.IsAny<Expression<Func<Employee, bool>>>())).Returns(Task.FromResult(true));
            // Act
            var result = await controller.Post(EmployeeData.GetNewEmployee());

            //Assert
            mockRepo.Verify(repo => repo.Add(It.IsAny<Employee>()), Times.Never);
            Assert.IsType<BadRequestObjectResult>(result);
        }

        [Fact]
        public async Task Get_Returns_All_Employees()
        {
            // Arrange
            mockRepo.Setup(repo => repo.GetAsync()).Returns(Task.FromResult(EmployeeData.GetEmployees()));


            // Act
            var result = await controller.GetAllAsync();

            // Assert
            var okResult = Assert.IsType<OkObjectResult>(result);
            Assert.IsType<List<EmployeeDto>>(okResult.Value);
            mockRepo.Verify(repo => repo.GetAsync(), Times.AtMostOnce);
        }
             
       [Theory]
       [InlineData(1)]
        public async Task Get_ReturnsHttpOkResult_ForValidEmployeeId(int empId)
        {
            // Arrange
            string employeeName = "testEmployee";
            mockRepo.Setup(repo => repo.GetAsync(It.IsAny<Expression<Func<Employee, bool>>>())).Returns(Task.FromResult(EmployeeData.GetEmployees().Where(t => t.EmpId == empId).ToList()));

            // Act
            var result = await controller.Get(empId);

            // Assert
            var okResult = Assert.IsType<OkObjectResult>(result);
            var emp = Assert.IsType<List<EmployeeDto>>(okResult.Value).FirstOrDefault();
            mockRepo.Verify(repo => repo.GetAsync(It.IsAny<Expression<Func<Employee, bool>>>()), Times.Once);
            Assert.True(emp.EmpName == employeeName);
        }
        [Fact]
        public async Task Get_ReturnsBadRequestResult_ForInValidEmpId()
        {
            // Arrange
           var empId = -1;
            mockRepo.Setup(repo => repo.GetAsync(It.IsAny<Expression<Func<Employee, bool>>>())).Returns(Task.FromResult(EmployeeData.GetEmployees().Where(t => t.EmpId == empId).ToList()));

            // Act
            var result = await controller.Get(empId);

            // Assert
            Assert.IsType<BadRequestObjectResult>(result);
            mockRepo.Verify(repo => repo.GetAsync(), Times.Never);
        }
      
       
        [Fact]
        public async Task Put_ReturnsBadRequestResult_WhenUpdatingInvalidNullEmployee()
        {
            // Arrange & Act

            // Act
            var result = await controller.Put(null);

            //Assert
            mockRepo.Verify(repo => repo.Update(It.IsAny<Employee>()), Times.Never);
            Assert.IsType<BadRequestObjectResult>(result);
        }
        [Fact]
        public async Task Put_ReturnsNotFoundRequest_WhenUpdatingInvalidEmployee()
        {
            // Arrange & Act
            mockRepo.Setup(repo => repo.AnyAsync(It.IsAny<Expression<Func<Employee, bool>>>())).Returns(Task.FromResult(false));
            // Act
            var result = await controller.Put(new EmployeeDto()
            {
                EmpId = 99
            });

            //Assert
            mockRepo.Verify(repo => repo.Update(It.IsAny<Employee>()), Times.Never);
            Assert.IsType<NotFoundResult>(result);
        }
        [Fact]
        public async Task Put_ReturnsOkResult_WhenUpdatingValidEmployee()
        {
            // Arrange & Act
            mockRepo.Setup(repo => repo.Update(It.IsAny<Employee>())).Verifiable();
            mockRepo.Setup(repo => repo.SaveAsync()).ReturnsAsync(1);
            mockRepo.Setup(repo => repo.AnyAsync(It.IsAny<Expression<Func<Employee, bool>>>())).Returns(Task.FromResult(true));
            // Act
            var result = await controller.Put(new EmployeeDto()
            {
                EmpId = 1
            });

            //Assert
            mockRepo.Verify(repo => repo.Update(It.IsAny<Employee>()), Times.Once);
            mockRepo.Verify(repo => repo.SaveAsync(), Times.Once);
            Assert.IsType<NoContentResult>(result);
        }
        [Fact]
        public async Task Delete_ReturnsHttpBadRequest_ForInvalidEmployee()
        {
            // Arrange
            int empId = 99;
            mockRepo.Setup(repo => repo.AnyAsync(It.IsAny<Expression<Func<Employee, bool>>>())).Returns(Task.FromResult(false));


            // Act
            var result = await controller.Delete(empId);

            // Assert
            Assert.IsType<BadRequestResult>(result);
        }

        [Fact]
        public async Task Delete_ReturnsNoContentResult_ForValidEmployee()
        {
            // Arrange
            int empId = 99;
            mockRepo.Setup(repo => repo.AnyAsync(It.IsAny<Expression<Func<Employee, bool>>>())).Returns(Task.FromResult(true));
            mockRepo.Setup(repo => repo.SaveAsync()).ReturnsAsync(1);

            // Act
            var result = await controller.Delete(empId);

            // Assert
            mockRepo.Verify(repo => repo.Delete(It.IsAny<int>()), Times.Once);
            mockRepo.Verify(repo => repo.SaveAsync(), Times.Once);
            Assert.IsType<NoContentResult>(result);
        }
       
    }
}

Code sample for .Net Core Web Api Controller Integration Tests

Integration testing ensures the different parts of an application work correctly together. Unlike unit testing, integration tests frequently involve application dependencies such as database.

To get set up integration tests in .Net Core, you'll need a test project, add reference to your .Net Core Api project and a test runner. Magic is in the test host which comes with ASP.NET Core that can be added to integration test project and can be used to host .Net Core Apps, serving http requests with out the need for a real web host.

Following sample uses xUnit, FluentAssertions and TestHost to compose a simple integration test.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Newtonsoft.Json;
using Xunit;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using FluentAssertions;
using Microsoft.AspNetCore;


namespace VisualStudio99.Tests.IntegrationTests
{
    public class LookupApiControllerIntegrationTests 
    {
        private readonly HttpClient _client;
        private readonly TestServer _server;
        
       
        public LookupApiControllerIntegrationTests()
        {
            //_server = new TestServer(WebHost.CreateDefaultBuilder().UseUrls("http://localhost:5000").Configure(_ => { }));
            _server = new TestServer(new WebHostBuilder().UseStartup<Startup>().UseUrls("http://localhost:5000"));
            _client = _server.CreateClient();

            _client.BaseAddress = new Uri("http://localhost:5000");
            _client.DefaultRequestHeaders.Accept.Clear();
            _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }
        [Fact]
        public async Task Lookup_Get_All_EmployeeTypes()
        {
                var response = await _client.GetAsync($"/api/lookup/employeetypes");
                // Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
                var responseString = await response.Content.ReadAsStringAsync();
                var employeeTypes = JsonConvert.DeserializeObject<IEnumerable<EmployeeTypeDto>>(responseString);
                employeeTypes.Count().Should().Equals(5);
                employeeTypes.Should().OnlyHaveUniqueItems();
                employeeTypes.Should().OnlyContain(x => !string.IsNullOrEmpty(x.EmployeeTypeCode));
        }
    }

.Net Application Architecture patterns for modern applications (docker, azure, mobile and cross platform)


Microsoft has posted some great content on .Net Application Architecture. This is similar to now stopped Microsoft Patterns and Practices guidance on architecting enterprise application using .Net stack or in general best practices guides to design solutions.

.Net Application Architecture is reincarnation of Microsoft Patterns and Practices where you can find some guides on building modern solutions using new patterns, frameworks, techniques and workflows like

  1.  Building Docker Applications, 
  2.  Azure Cloud ready applications
  3.  Modernizing Full .Net Framework apps to cross platform .Net Core apps
  4.  Xamarin Mobile Apps

These new design guides are extensive. You can download these guides along with a sample app to test new patterns

Go to: https://www.microsoft.com/net/learn/architecture

Enjoy!

Nicely curated keyboard shortcut cheat sheet for Visual Studio 2017



  • Useful key board shortcuts cheatsheet for visual studio 2017 published by Microsoft. 
  • Neatly captures most heavily used productivity improving keyboard shortcuts.
  • Provides side by side keyboard shortcuts for Resharper, Visual Studio 2017 and Visual Studio 2015
  • Click here to download pdf cheat sheet