Skip to content

Instantly share code, notes, and snippets.

@bitbonk
Last active November 30, 2015 18:15
Show Gist options
  • Save bitbonk/f48c460c5bd065606b55 to your computer and use it in GitHub Desktop.
Save bitbonk/f48c460c5bd065606b55 to your computer and use it in GitHub Desktop.
Can I have one Should() instead of two?
using System;
using System.Collections;
using System.Linq;
using FluentAssertions;
using Xunit;
public class ParserTests
{
[Theory,
InlineData(DataType.String, "foo", "foo"),
InlineData(DataType.Integer, "42", 42),
InlineData(DataType.ByteArray, "1,2,3", new byte[] { 1, 2, 3 }),
InlineData(DataType.ByteArray, "1,2,3", new double[] { 1, 2, 3 })]
public void ShouldAllEqual(DataType datatype, string dataToParse, object expectedResult)
{
var result = Parser.Parse(datatype, dataToParse);
result.ShouldBeEquivalentTo(expectedResult, o => o.RespectingRuntimeTypes());
}
}
// Simplified SUT:
public enum DataType
{
String,
Integer,
ByteArray
}
public static class Parser
{
public static object Parse(DataType datatype, string dataToParse)
{
switch (datatype)
{
case DataType.Integer:
return int.Parse(dataToParse);
case DataType.ByteArray:
return
dataToParse.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(byte.Parse)
.ToList();
default:
return dataToParse;
}
}
}
@dennisdoomen
Copy link

You could try result.ShouldBeEquivalentTo(expectedResult, options => options.IncludingRuntimeType);

@bitbonk
Copy link
Author

bitbonk commented Nov 30, 2015

You probably meant RespectingRuntimeTypes() that will not fail the last test (that I have just added) but it should.

@dennisdoomen
Copy link

Yes, that's the one I meant. But I suspect it's doing the automatic conversion. The only way to remove that one is to modify it on a global level using the AssertionOptions.EquivalencySteps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment