Test Driven-Development-(TDD)

SOAP WEB SERVICES IN .NET CORE
Method chaining

Table of Contents

Test Class

				
					using System;
using FluentAssertions;
using Xunit;

namespace Academy.Domain.Tests
{
    public class CourseTests
    {
        [Fact]
        public void Constructor_ShouldConstructCourseProperly()
        {
            const int id = 1;
            const string name = "tdd & bdd";
            const bool isOnline = true;
            const double tuition = 600;
            const string instructor = "hossein";
            var courseBuilder = new CourseTestBuilder();

            var course = courseBuilder.Build();

            course.Id.Should().Be(id);
            course.Name.Should().Be(name);
            course.IsOnline.Should().Be(isOnline);
            course.Tuition.Should().Be(tuition);
            course.Instructor.Should().Be(instructor);
            course.Sections.Should().BeEmpty();
            //Assert.Equal(id, course.Id);
            //Assert.Equal(name, course.Name);
            //Assert.Equal(isOnline, course.IsOnline);
            //Assert.Equal(tuition, course.Tuition);
        }

        [Fact]
        public void Constructor_ShouldThrowException_WhenNameIsNotProvided()
        {

            const int id = 1;
            const string name = "";
            const bool isOnline = true;
            const double tuition = 600;
            const string instructor = "Hadi";

            Action course = () => new Course(id, name, isOnline, tuition,instructor);

            Assert.Throws<CourseNameIsInvalidException>(course);
        }

        [Fact]
        public void Constructor_ShouldThrowException_WhenTuitionIsNotProvided()
        {
            var courseBuilder = new CourseTestBuilder();

            Action course = () => courseBuilder.WithTuition(0).Build();

            course.Should().ThrowExactly<CourseTuitionIsInvalidException>();
            //Assert.Throws<Exception>(Course);
        }

        [Fact]
        public void AddSection_ShouldAddNewSectionToSections_WhenIdAndNamePassed()
        {
            //arrange
            var courseBuilder = new CourseTestBuilder();
            var course = courseBuilder.Build();
            var sectionToAdd = SectionFactory.Create();

            //act
            course.AddSection(sectionToAdd);

            //assert
            course.Sections.Should().ContainEquivalentOf(sectionToAdd);
        }
    }
}
				
			

Handler

				
					using System.Collections.Generic;
using Academy.Domain.Tests;

namespace Academy.Domain
{
    public class Course
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public bool IsOnline { get; set; }
        public double Tuition { get; set; }
        public string Instructor { get; set; }
        public List<Section> Sections { get; set; }

        public Course(int id, string name, bool isOnline, double tuition, string instructor)
        {
            GuardAgainstInvalidName(name);
            GuardAgainstInvalidTuition(tuition);

            Id = id;
            Name = name;
            IsOnline = isOnline;
            Tuition = tuition;
            Instructor = instructor;
            Sections = new List<Section>();
        }

        private static void GuardAgainstInvalidName(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
                throw new CourseNameIsInvalidException();
        }

        private static void GuardAgainstInvalidTuition(double tuition)
        {
            if (tuition <= 0)
                throw new CourseTuitionIsInvalidException();
        }

        public void AddSection(Section section)
        {
            Sections.Add(section);
        }
    }
}
				
			

CourseTestBuilder (using : Builder Pattern & Method Chaining)

				
					namespace Academy.Domain.Tests
{
    public class CourseTestBuilder
    {
        private const int Id = 1;
        private string _name = "tdd & bdd";
        private const bool IsOnline = true;
        private double _tuition = 600;
        private const string Instructor = "hossein";

        public CourseTestBuilder WithName(string name)
        {
            _name = name;
            return this;
        }

        public CourseTestBuilder WithTuition(double tuition)
        {
            _tuition = tuition;
            return this;
        }

        public Course Build()
        {
            return new Course(Id, _name, IsOnline, _tuition, Instructor);
        }
    }
}