bilal-fazlani / commanddotnet

A modern framework for building modern CLI apps
https://commanddotnet.bilal-fazlani.com
MIT License
569 stars 30 forks source link

I have problem reproducing your test Bilal #397

Closed atari-monk closed 2 years ago

atari-monk commented 2 years ago

I have project with your calculator example next i have project (xunit) with your test net6. It is hard to provide all info It;s in my repo console. Your test fails I have no clue why i am stuck on this am i the only one ?

drewburlingame commented 2 years ago

hi @krzm, there have been some changes to the framework and documentation has fallen behind. It's something I'm working on in a branch right now. I'll have more getting started examples and they'll be snipets of code that's continually tested.

I'll need more info to help you. How is it failing? Are their compilation errors or is it nit running? Can you share any code?

atari-monk commented 2 years ago
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="CommandDotNet" Version="4.3.0" />
  </ItemGroup>

</Project>
using CommandDotNet;
using System;

namespace ConsoleNS.Modern.Lib
{
    [Command(Description = "Performs mathematical calculations")]
    public class Calculator
    {
        [Command(Description = "Adds two numbers")]
        public void Add(int value1, int value2)
        {
            Console.WriteLine($"Answer:  {value1 + value2}");
        }

        [Command(Description = "Subtracts two numbers")]
        public void Subtract(int value1, int value2)
        {
            Console.WriteLine($"Answer:  {value1 - value2}");
        }
    }
}

This compiles OK

atari-monk commented 2 years ago
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="CommandDotNet" Version="4.3.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Console.Modern.Lib\Console.Modern.Lib.csproj" />
  </ItemGroup>

</Project>
using CommandDotNet;
using ConsoleNS.Modern.Lib;

namespace ConsoleApp.Modern
{
    class Program
    {
        static int Main(string[] args)
        {
            return new AppRunner<Calculator>().Run(args);
        }
    }
}

This compiled and run ok with commands

atari-monk commented 2 years ago
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="CommandDotNet.TestTools" Version="3.1.2" />
    <PackageReference Include="FluentAssertions" Version="6.2.0" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="3.0.2">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Console.Modern.Lib\Console.Modern.Lib.csproj" />
  </ItemGroup>

</Project>
using CommandDotNet;
using CommandDotNet.TestTools;
using ConsoleNS.Modern.Lib;
using FluentAssertions;
using Xunit;

namespace Console.Modern.Tests
{
    public class PipedInputTests
    {
        [Fact]
        public void Add_Given2Numbers_Should_OutputSum()
        {
            var result = new AppRunner<Calculator>().RunInMem("Add 40 20");
            result.ExitCode.Should().Be(0);
            result.Console.Out.Should().Be(@"60");
        }
    }
}

This fails the test

Console.Modern.Tests.PipedInputTests.Add_Given2Numbers_Should_OutputSum: Outcome: Failed Error Message: Expected result.Console.Out to be "60", but found . Stack Trace: at FluentAssertions.Execution.XUnit2TestFramework.Throw(String message) at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message) at FluentAssertions.Execution.DefaultAssertionStrategy.HandleFailure(String message) at FluentAssertions.Execution.AssertionScope.FailWith(Func1 failReasonFunc) at FluentAssertions.Execution.AssertionScope.FailWith(Func1 failReasonFunc) at FluentAssertions.Execution.AssertionScope.FailWith(String message, Object[] args) at FluentAssertions.Primitives.ObjectAssertions`2.Be(TSubject expected, String because, Object[] becauseArgs) at Console.Modern.Tests.PipedInputTests.Add_Given2Numbers_Should_OutputSum()

Prolly just outdated doc example. Prolly tests in repo works.

drewburlingame commented 2 years ago

Ah yeah, that's because you're using the System Console instead of IConsole.

Make this change

        [Command(Description = "Adds two numbers")]
        public void Add(IConsole console, int value1, int value2)
        {
            console.WriteLine($"Answer:  {value1 + value2}");
        }

        [Command(Description = "Subtracts two numbers")]
        public void Subtract(IConsole console, int value1, int value2)
        {
            console.WriteLine($"Answer:  {value1 - value2}");
        }

I'm calling this out more clearly in the new Getting Started guides

drewburlingame commented 2 years ago

And in the new TestTools released today, I've added an InterceptSystemConsoleWrites method so you could keep using the System Console.

The test would change to be

        [Fact]
        public void Add_Given2Numbers_Should_OutputSum()
        {
            var result = new AppRunner<Calculator>().InterceptSystemConsoleWrites().RunInMem("Add 40 20");
            result.ExitCode.Should().Be(0);
            result.Console.Out.Should().Be(@"60");
        }
drewburlingame commented 2 years ago

I'm going to convert this issue to a discussion