Alıştırma - Copilot ile kodu test etme

Tamamlandı

Copilot'un birim testleri oluşturma özelliği geliştiriciler için harika bir zaman tasarrufu sağlar. Bu alıştırmada, Trie projesi için birim testleri oluşturmak üzere Copilot kullanacaksınız. Haydi başlayalım!

Kodu test etmek için Copilot kullanma

Bu alıştırmada, trie yöntemleri için temel testler oluşturmak üzere Copilot'u kullanacaksınız.

  1. TrieDictionaryTest altında TrieTests.cs dosyasını açma

  2. TrieTest sınıfına aşağıdaki kodu girin:

    // Test that a word is inserted in the trie
    [TestMethod]
    
  3. Yeni bir satır girin, ardından Copilot'un kod oluşturmasını bekleyin

    Copilot, bir sözcük ekleyen ve sözcüğün eklendiğini onaylayan bir kod oluşturmalıdır.

  4. Yeni bir satıra aşağıdaki kodu girin:

    // Test that a word is deleted from the trie
    [TestMethod]
    
  5. Yeni bir satır girin, ardından Copilot'un kod oluşturmasını bekleyin

    Copilot bir sözcük ekleyen ve sözcüğün silindiğini onaylayan bir kod oluşturmalıdır.

  6. Aşağıdaki istemlerle adımları yineleyin:

    // Test that a word is not inserted twice in the trie
    
    // Test that a word is deleted from the trie
    
    // Test that a word is not deleted from the trie if it is not present
    
    // Test that a word is deleted from the trie if it is a prefix of another word
    

    Test kodu oluşturmaya devam ettikçe Copilot'un istemi, [TestMethod] saplamasını ve kodu otomatik olarak tamamladığını fark edebilirsiniz. Copilot'un ihtiyacınız olan kodu tahmin edebilmesi, birim testlerinin daha hızlı oluşturulmasını sağlar. Oluşturulan onayların doğru olduğundan emin olun.

    Ancak, Copilot her zaman ihtiyacınız olan testi tam olarak tahmin etmeyebilir. Oluşturulan kodu gözden geçirmeniz ve testte değişiklik yapmanız veya daha belirgin bir istem kullanmanız gerekebilir

  7. Yeni bir satıra aşağıdaki kod açıklamasını girin:

    // Test AutoSuggest for the prefix "cat" not present in the 
    // trie containing "catastrophe", "catatonic", and "caterpillar"
    
  8. Yeni bir satır girin ve Copilot'un kod oluşturmasını bekleyin

    Oluşturulan testin aşağıdakine benzer olup olmadığını denetleyin ve gerekli değişiklikleri yapın:

    [TestMethod]
    public void TestAutoSuggest()
    {
        Trie dictionary = new Trie();
        dictionary.Insert("catastrophe");
        dictionary.Insert("catatonic");
        dictionary.Insert("caterpillar");
        List<string> suggestions = dictionary.AutoSuggest("cat");
        Assert.AreEqual(3, suggestions.Count);
        Assert.AreEqual("catastrophe", suggestions[0]);
        Assert.AreEqual("catatonic", suggestions[1]);
        Assert.AreEqual("caterpillar", suggestions[2]);
    }
    
  9. Yeni bir satıra aşağıdaki kod açıklamasını girin:

    // Test GetSpellingSuggestions for a word not present in the trie
    
  10. Yeni bir satır girin ve Copilot'un kod oluşturmasını bekleyin

    Oluşturulan testin aşağıdakine benzer olup olmadığını denetleyin ve gerekli değişiklikleri yapın:

    [TestMethod]
    public void TestGetSpellingSuggestions()
    {
        Trie dictionary = new Trie();
        dictionary.Insert("cat");
        dictionary.Insert("caterpillar");
        dictionary.Insert("catastrophe");
        List<string> suggestions = dictionary.GetSpellingSuggestions("caterpiller");
        Assert.AreEqual(1, suggestions.Count);
        Assert.AreEqual("caterpillar", suggestions[0]);
    }
    

Çalışmanızı kontrol edin

Bu görevde, Copilot ile oluşturduğunuz yöntemleri test eder ve beklendiği gibi çalıştıklarını doğrularsınız.

  1. Dosya gezgininde, TrieTest.cs dosyasına sağ tıklayın ve Tümleşik Terminalde Aç'a tıklayın

  2. Testleri çalıştırmak için dotnet test girin.

  3. Tüm testlerin geçtiğini doğrulayın.

    Passed!  - Failed:     0, Passed:     7, Skipped:     0, Total:     7, Duration: 638 ms - TrieDictionaryTest.dll (net7.0)
    

    Testleriniz farklı sonuçlar üretirse, hatayı bulmak ve güncelleştirmeler yapmak için kodunuzu gözden geçirin. Sorunu düzeltip düzeltmediğinize bakmak için kodu yeniden çalıştırın. Kodunuz beklenen sonuçları elde edene kadar kodunuzu güncelleştirmeye ve çalıştırmaya devam edin.