Angular UnitTesting Multiple CSS Value

it('Displays First Day of Week correctly', () => {

    fixture.detectChanges();

    let debugElement: DebugElement[] = fixture.debugElement.queryAll(By.css('.date'));
   
    let weekDays = ['15 Mon','16 Tue','17 Wed','18 Thu','19 Fri'];
    let i = 0;
    debugElement.forEach(x => {
      expect(x.nativeElement.textContent).toBe(weekDays[i]);
      i++;
    });

  });
It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Fixed Angular Material and Karma Error

If you are having trouble with Unknown ‘Angular Material Module’ element in your Angular Unit Test.
This could be the fix.

Add the Angular Material Module to the imports in beforeEach.

import { DebugElement } from '@angular/core';
import { By } from '@angular/platform-browser';

import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { MatSidenavModule, MatCardModule } from '@angular/material';
import { DayscheduleComponent } from './../dayschedule/dayschedule.component';
import { WeekscheduleComponent  } from './../weekschedule/weekschedule.component';

import * as moment from 'moment';



describe('SchedulesComponent', () => {
  let component: SchedulesComponent;
  let fixture: ComponentFixture<SchedulesComponent>;

  let debugElement: DebugElement;
  let htmlElement: HTMLElement;

  beforeEach(
    async(() => {
      TestBed.configureTestingModule({
        declarations: [SchedulesComponent, DayscheduleComponent, WeekscheduleComponent],
        imports: [ MatSidenavModule, MatCardModule, HttpModule, BrowserModule, BrowserAnimationsModule ]
      }).compileComponents();
    })
  );
It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Basic Angular UnitTest Template

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { DebugElement } from '@angular/core';
import { CounterComponent } from './counter.component';
import { By } from '@angular/platform-browser';
describe('CounterComponent', () => {
  let component: CounterComponent;
  let fixture: ComponentFixture<CounterComponent>;
  let debugElement: DebugElement;
  let htmlElement: HTMLElement;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
    declarations: [CounterComponent]
    }).compileComponents();
  }));

  beforeEach(() => {
     fixture = TestBed.createComponent(CounterComponent);
     component = fixture.componentInstance;

     fixture.detectChanges();

     debugElement = fixture.debugElement.query(By.css('p'));
     htmlElement = debugElement.nativeElement;
  });

  it('should increment the counter number by one', () => {
    // Arrange
    const initialValue = component.counter;

    // Act
    component.increment();
    fixture.detectChanges();
    const newValue = component.counter;

    // Assert
    expect(newValue).toBeGreaterThan(initialValue);
  });

  it('should decrement the counter number by one', () => {
    // Arrange
    const initialValue = component.counter;

    // Act
    component.decrement();
    fixture.detectChanges();
    const newValue = component.counter;

    // Assert
    expect(newValue).toBeLessThan(initialValue);
  });

  it('should display the counter number on screen, after being incremented by one', () => {

    component.increment();
    fixture.detectChanges();

    // Assert that the test on screen is of Number: 1
    expect(htmlElement.textContent).toEqual('Number: 2');
  });

  it('should display the counter number on screen, after being decremented by one', () => {

    component.decrement();
    fixture.detectChanges();

    // Assert that the test on screen is of Number: 1
    expect(htmlElement.textContent).toEqual('Number: 0');
  });

  it('should display the current number of the counter', () => {
    // Assert that the test on screenis of Number: 1
    expect(htmlElement.textContent).toEqual('Number: 1');

  });

});
It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

C#Fluent Assertion with InMemoryDatabase UnitTesting

 // assert each employee has morning and afternoon shifts
 var employees = context.Employees;
 foreach (var emp in employees)
 {
     var empShifts = context.EmployeeShifts.Where(x => x.EmployeeId == emp.Id).ToList();
     empShifts.Count().Should().Be(10);
     empShifts.Where(x => x.ShiftSchedule == ENUMERATIONS.ShiftSchedule.firstHalf).Count().Should().Be(5);
     empShifts.Where(x => x.ShiftSchedule == ENUMERATIONS.ShiftSchedule.secondHalf).Count().Should().Be(5);
  }

  // assert if no consecutive days shift of each employee
  foreach (var emp in employees)
  {
      var empShifts = context.EmployeeShifts.Where(x => x.EmployeeId == emp.Id).ToList();
                 
       foreach (var shift in empShifts)
       {
            var shiftDate = shift.Date;

            var nextDate = shift.Date.AddDays(1);
            var prevDate = shift.Date.AddDays(-1);

            var nextShift = empShifts.Where(x => x.Date == nextDate).FirstOrDefault();
            var prevShift = empShifts.Where(x => x.Date == prevDate).FirstOrDefault();
                       
            nextShift.Should().BeNull();
            prevShift.Should().BeNull();
                       
       }


  }
It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Delegates and Functional C# example again using Extension Methods

public class Planet
{
    public string Name { get; set; }
}

public static Planet AddXtoName(this Planet o)
{
    o.Name = o.Name + "X";
    return o;
}

public static Planet AddYToName(this Planet o)
{
    o.Name = o.Name + "Y";
    return o;
}

public static void Execute(this Planet o, Action cmd)
{
    cmd();
}

public static void Execute2(this Planet o, Action<string> cmd)
{
    cmd(o.Name);
}

public static int Execute3(this Planet o, Func<string, int> cmd)
{
    return cmd(o.Name);
}

var px = new Planet();
px.Name = "Jupiter";
px.AddXtoName().AddYToName();

px.AddYToName().AddXtoName();
px.Execute(() =>
{
    Console.WriteLine("Execute this!");
});

px.Execute2((x) =>
{
    Console.WriteLine($"Name of Planet is: {x}");
});

px.Execute2((x) =>
{
    Console.WriteLine($"This is also another execution of Exe2 for Name of: {x}");
});

var len = px.Execute3((x) =>
{
    return x.Length;
});

var len2 = px.Execute3((x) =>
{
    return x.Length + 100;
});

Console.WriteLine(len);
Console.WriteLine(len2);
Console.WriteLine("");
It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Example of Functional C# using extension Methods

public class Calculor {
    public int Total { get; set; }
}
public class Calculator {
    public int Total { get; set; }
}

public static Calculator Add(this Calculator c, int number) {
    c.Total += number;
    return c;
}
public static Calculator Minus(this Calculator c, int number) {
    c.Total -= number;
    return c;
}
public static Calculator Times(this Calculator c, int number) {
    c.Total *= number;
    return c;
}
public static Calculator Divide(this Calculator c, int number) {
    c.Total = c.Total / number;
    return c;
}

var c = new Calculator();
c.Add(100).Add(50);

Console.WriteLine(c.Total);
150
c.Minus(30);
Console.WriteLine(c.Total);
120
public static object Addx<T>(this T c, int number) {
    object obj = (object)c;

    var p = obj.GetType().GetProperty("Total");
    object itemValue = p.GetValue(obj, null);
    int total = (int)itemValue;
    total += number;
    p.SetValue(obj, total, null);
    return obj;
}

var cc = new Calculator();
cc.Addx<Calculator>(888);
Console.WriteLine(cc.Total);

Console.WriteLine("");
888

public class CalculatorX{
    public int Total { get; set; }
}
var cx = new CalculatorX();
cx.Addx<CalculatorX>(777);
Console.WriteLine(cx.Total);
Console.WriteLine("");
It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Add-Migration Entity Framework Core (ef core) Nothing Happens

If you are out there wondering what is going on to your

add-migration

or

update-database

, same thing happened to me.
I have multiple startup projects in my Visual Studio 2017. The fix is to use your main WebApi / WebApp Project as the start up project
first before running the

add-migration

again and walahhh you are good to go kid. Your welcome.

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Func Type and Extension Methods in C#

Here is a sample code Func is useful and Extension Methods.

using System;

class MainClass {
  public static void Main (string[] args) {
   
    var Gouko = new Hero();
    Gouko.Name = "Gouko";
    Gouko.Energy = 100;
    Gouko.LevelUp = x => x.RashingGun().RayGun().KamihamiWave();
    var powerLevel = Gouko.LevelUp(10);
    Console.WriteLine(powerLevel);
   
    var Naruto = new Hero();
    Naruto.Name = "Naruto";
    Naruto.Energy = 50;
    Naruto.LevelUp = x => {
      var z = 5;
      return z * x;
    };
    var naruloLevel = Naruto.LevelUp(5);
    Console.WriteLine(naruloLevel);
  }
 
}

public interface IHero{
}

public class Hero : IHero
{
  public string Name { get; set; }
  public int Energy{ get; set; }
  public Func<int,int> LevelUp { get; set; }
 
}
 
public static class HeroPipe{
  public static int RashingGun(this int power){
    return power * 2;
  }
  public static int RayGun(this int power){
    return power * 10;
  }
  public static int KamihamiWave(this int power){
    return power * 100;
  }
}

Play with the code: Repl.it

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

C# Linq Grouping and Distinct

var px = parents.SelectMany(p => p.Childs.Select(c => new {
                        number = c.Number,
                        parent = p
                 })).GroupBy(g => g.number).Distinct().Select(s => s.First())
                .GroupBy(g => g.parent)
                .Select(s => s.First());
It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn