Search code examples
c#visual-studiounit-testingc#-4.0nunit

I get the Error CS0104 'Assert' is an ambiguous reference between 'NUnit.Framework.Assert' and 'Microsoft.VisualStudio.TestTools.UnitTesting.Assert


I am currently making a simple unit test for my Recipe storage application to test my calorie calculation. However, I keep running into the above issue and am unsure as what I can do to solve it. The error is found on my My Assert.AreEqual(expectedTotalCalories, actualTotalCalories); line. Any help would be much appreciated. The code is below:

using NUnit.Framework;
using RecipeApplication;

namespace RecipeApp_UnitTest
{
    [TestFixture]
    public class RecipeTests
    {
        [Test]
        public void TestTotalCaloriesCalculation()
        {
            // Arrange
            Recipe recipe = new Recipe();

            // Add ingredients
            recipe.EnterRecipeDetails();

            // Calculate expected total calories
            double expectedTotalCalories = 0;
            foreach (var ingredient in recipe.ingredients)
            {
                expectedTotalCalories += ingredient.Calories * ingredient.Quantity;
            }

            // Act
            double actualTotalCalories = recipe.totalCalories;

            // Assert
            Assert.AreEqual(expectedTotalCalories, actualTotalCalories);
        }
    }
}

I have tried Nunit internal.


Solution

  • Your program contains using directives for two namespaces and your code references a name that appears in both namespaces.

    Try to specify the using with using NUnit.Framework.Assert;

    In your IDE you can also import like this: select import from ide, this way you can select where the import came from. You need just to click on top the type you want to import and click on the lamp in the left (if you are using rider)