Skip to content

Using DotNetFiddle to Report a Bug

ColmBhandal edited this page Feb 8, 2022 · 7 revisions

All reported bugs should include a link to a DotNetFiddle code snippet reproducing the issue. The code should clearly demonstrate what the issue is by reproducing it. If necessary, comments should be added to annotate where the code is failing and what the expectation is. If possible, the code should be structured as a unit test following the AAA test pattern as follows:

  • Arrange: in this section build the objects necessary for reproducing the bug
  • Act: Do the action that is causing the bug
  • Assert: Assert that some condition holds

Note: sometimes it's not possible to have 3 separate phases and 2 or more phases need to be merged e.g. when you are asserting that some action throws an exception, usually the "Act" and "Assert" phases are merged.

Given that you are reporting a bug, the assert should throw an exception, demonstrating that the code isn't behaving as expected. Also, to facilitate easy translation of your bug report into a unit test, it's advisable to structure your code just like a test would be structured, including the naming convention of GIVEN_X_WHEN_Y_THEN_Z.

See the example below & use it if you like as a starting point to report your bug.

NB: The version of the C# Extras NuGet package chosen in the DotNetFiddle code snippet should be the same as the version for which you are reporting the bug. This means you may need to change the C# Extras version from the below example if it isn't the version for which you're reporting the bug.

Example

Here is an example of a DotNetFiddle reproduction for this issue. The code is below. Notice how the code is structured as a unit test. Note: you need to use the NUnit package here to do the assert.

using System;
using CsharpExtras.Api;
using CsharpExtras.Extensions;
using CsharpExtras.ValidatedType.Numeric.Integer;
using NUnit.Framework;

public class Program
{
	public static void Main()
	{	
		GIVEN_SparseArray_WHEN_SetToDefault_THEN_GetReturnsDefault();
	}
	
	private static void GIVEN_SparseArray_WHEN_SetToDefault_THEN_GetReturnsDefault()
	{
		//Arrange
		var api = new CsharpExtrasApi();	
		var array = api.NewSparseArrayBuilder<string>((PositiveInteger) 3, "").Build();
		array[1, 2, 3] = "Initial Non-default value";
		
		//Act
		array[1, 2, 3] = "";
		
		//Assert
		Assert.AreEqual("", array[1, 2, 3]);
	}
}

Note: the Main method is necessary as an entry point for DotNetFiddle, hence the separation of the code into two methods.

Running this code results in the below output. The assertion exception thrown demonstrates the bug:

Unhandled exception. NUnit.Framework.AssertionException:   Expected string length 0 but was 25. Strings differ at index 0.
  Expected: <string.Empty>
  But was:  "Initial Non-default value"
  -----------^

   at NUnit.Framework.Assert.ReportFailure(String message)
   at NUnit.Framework.Assert.ReportFailure(ConstraintResult result, String message, Object[] args)
   at NUnit.Framework.Assert.That[TActual](TActual actual, IResolveConstraint expression, String message, Object[] args)
   at NUnit.Framework.Assert.AreEqual(Object expected, Object actual)
   at Program.GIVEN_SparseArray_WHEN_SetToDefault_THEN_GetReturnsDefault()
   at Program.Main()
Command terminated by signal 6

Clone this wiki locally