1

I am building a card game using Godot 4.5, with C#. I am trying to unit test a Card object that inherits from Node2D, but when I attempt to run the test, I get the following exception:

Exception has occurred: CLR/System.AccessViolationException
An unhandled exception of type 'System.AccessViolationException' occurred in GodotSharp.dll: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
<Cannot evaluate the exception stack trace>

This is the Card object that I am trying to test (not all the code has been included for brevity, but assume that every variable/constant/method/enum etc is declared correctly):

using Godot;
using System;
using System.Collections.Generic;

/// <summary>
/// A playing card.
/// </summary>
public partial class Card: Node2D
{
    public CardNumber CardNumber { get; set; }
    public CardSuit CardSuit { get; set; }

    public double CalculateCardScore(TrumpSuit trump, int deckSize = 52)
    {
        var cardsBeaten = (int)this.CardNumber - 2;

        return cardsBeaten;
    }
}

I have created this unit test for the Card's CalculateCardScore() method:

namespace BlogTheGameUnitTests;

public class CardTests
{
    [Fact]
    public void CardCalculateCardScore_NoTrumps_DefaultDeckSize()
    {
        // Arrange
        Card testCard = new() { CardNumber = CardNumber.SEVEN, CardSuit = CardSuit.HEART };
        double expectedScore = (double)(((int)testCard.CardNumber - 2) / 51);

        // Act
        double actualScore = testCard.CalculateCardScore(TrumpSuit.NO_TRUMP);

        // Assert
        Assert.Equal(1, actualScore);
    }
}

I was expecting this test to fail (the method being tested is a work in progress, so using TDD I would eventually get the test to pass), instead of throwing a System.AccessViolationException.

I also tried using GDUnit4, but I got the same result.

After searching StackOverFlow and Googling, I only found one reference related to my specific problem - this Reddit post. It doesn't have an answer that addresses it, but the question seems to hint at the C++ core of the Godot engine not running when running these unit tests, leading to this exception when trying to instantiate any objects inheriting from Node2D.

Perhaps there is something I'm not doing that I should be doing here? How do I prevent this exception occurring so I can run my unit test correctly?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.