Exercise - Test code with Copilot

Nicolas Anderson 105 Reputation points
2024-06-14T17:18:55.2633333+00:00

Something is terribly wrong with this project setup. nothing works for this testing. I tried everything. https://learn.microsoft.com/en-us/training/modules/build-auto-suggest-engine-copilot/7-test-code-with-copilot

This question is related to the following Learning Module

Windows Training
Windows Training
Windows: A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.Training: Instruction to develop new skills.
24 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Pradeep M 3,830 Reputation points Microsoft Vendor
    2024-06-18T05:57:28.75+00:00

    Hi Nicolas Anderson,

    Thank you for reaching out to Microsoft Q & A forum.   

    1.Open the TrieTests.cs File 

    Open the TrieTests.cs file located under the TrieDictionaryTest directory in your project. 

    2.Insert a New Test Method to Test Insertion 

    In the TrieTest class, enter the following code: 

    // Test that a word is inserted in the trie
    [TestMethod]
    

    Press Enter and wait for Copilot to suggest the code for the test method. It should generate something like: 

    public void TestInsert()
    {
        Trie trie = new Trie();
        trie.Insert("apple");
        Assert.IsTrue(trie.Contains("apple"));
    }
    
    

    3.Insert a New Test Method to Test Deletion 

    On a new line, enter the following code: 

    // Test that a word is deleted from the trie
    [TestMethod]
    

    Press Enter and wait for Copilot to suggest the code for the test method. It should generate something like: 

    public void TestDelete()
    {
        Trie trie = new Trie();
        trie.Insert("apple");
        trie.Delete("apple");
        Assert.IsFalse(trie.Contains("apple"));
    }
    
    

    Similarly write all the test cases 

    The finalized TrieTests.cs file appears as follows: 

    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using System.Collections.Generic;
    namespace TrieDictionaryTest
    {
        [TestClass]
        public class TrieTests
        {
            [TestMethod]
            // Test that a word is inserted in the trie
            public void TestInsert()
            {
                Trie trie = new Trie();
                trie.Insert("apple");
                Assert.IsTrue(trie.Contains("apple"));
            }
            [TestMethod]
            // Test that a word is deleted from the trie
            public void TestDelete()
            {
                Trie trie = new Trie();
                trie.Insert("apple");
                trie.Delete("apple");
                Assert.IsFalse(trie.Contains("apple"));
            }
            [TestMethod]
            // Test that a word is not inserted twice in the trie
            public void TestInsertTwice()
            {
                Trie trie = new Trie();
                trie.Insert("apple");
                trie.Insert("apple");
                Assert.AreEqual(1, trie.CountOccurrences("apple"));
            }
            [TestMethod]
            // Test that a word is deleted from the trie if it exists
            public void TestDeleteIfExists()
            {
                Trie trie = new Trie();
                trie.Insert("apple");
                Assert.IsTrue(trie.Delete("apple"));
            }
            [TestMethod]
            // Test that a word is not deleted from the trie if it is not present
            public void TestDeleteIfNotPresent()
            {
                Trie trie = new Trie();
                Assert.IsFalse(trie.Delete("apple"));
            }
            [TestMethod]
            // Test that a word is deleted from the trie if it is a prefix of another word
            public void TestDeletePrefix()
            {
                Trie trie = new Trie();
                trie.Insert("apple");
                trie.Insert("app");
                Assert.IsTrue(trie.Delete("app"));
                Assert.IsFalse(trie.Contains("app"));
                Assert.IsTrue(trie.Contains("apple"));
            }
            [TestMethod]
            // Test AutoSuggest for the prefix "cat" not present in the 
            // trie containing "catastrophe", "catatonic", and "caterpillar"
            public void TestAutoSuggest()
            {
                Trie trie = new Trie();
                trie.Insert("catastrophe");
                trie.Insert("catatonic");
                trie.Insert("caterpillar");
                List<string> suggestions = trie.AutoSuggest("cat");
                Assert.AreEqual(3, suggestions.Count);
                Assert.AreEqual("catastrophe", suggestions[0]);
                Assert.AreEqual("catatonic", suggestions[1]);
                Assert.AreEqual("caterpillar", suggestions[2]);
            }
            [TestMethod]
            // Test GetSpellingSuggestions for a word not present in the trie
            public void TestGetSpellingSuggestions()
            {
                Trie trie = new Trie();
                trie.Insert("cat");
                trie.Insert("caterpillar");
                trie.Insert("catastrophe");
                List<string> suggestions = trie.GetSpellingSuggestions("caterpiller");
                Assert.AreEqual(1, suggestions.Count);
                Assert.AreEqual("caterpillar", suggestions[0]);
            }
        }
    }
    
    

    4.Verify the Tests 

    In the file explorer, right-click the TrieTest.cs file and click Open in Integrated Terminal. 

    Enter “dotnet test” to run the tests. 

    If your tests produce different results, review your code to find the error and make updates. Run the code again to see if you've fixed the problem. Continue updating and running your code until your code produces the expected results. 

    Please feel free to contact us if you have any additional questions.    

    If you have found the answer provided to be helpful, please click on the "Accept answer/Upvote" button so that it is useful for other members in the Microsoft Q&A community.       

    Thank you. 


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.