We need to add unit tests for the Balance query to ensure it works as expected for different currencyTicker values and properly handles missing inputs.
Create BalanceQueryTest.cs with Initial integration Tests
Path: Mimir.Tests/QueryTests/BalanceQueryTest.cs
public class BalanceQueryTest
{
[Fact]
public async Task GraphQL_Query_Balance_CRYSTAL_Returns_CorrectValue()
{
}
[Fact]
public async Task GraphQL_Query_Balance_NCG_Returns_CorrectValue()
{
}
[Fact]
public async Task GraphQL_Query_Balance_Throws_When_No_Inputs_Provided()
{
}
}
Mocking IServiceProvider with Mock BalanceRepository
Create IBalanceRepository Interface:
Add the IBalanceRepository interface and apply it to BalanceRepository.
Make BalanceRepository.GetByAddressAsync Virtual:
Modify the GetByAddressAsync method to be virtual to allow mocking.
Mock the Repository for Different Scenarios:
[Fact]
public async Task GraphQL_Query_Balance_CRYSTAL_Returns_CorrectValue()
{
var mockAddress = new Address("0x0000000000000000000000000000000000000000");
var mockRepo = new Mock<IBalanceRepository>();
mockRepo
.Setup(repo => repo.GetByAddressAsync("CRYSTAL".ToCurrency(), mockAddress))
.ReturnsAsync(new Balance { Currency = "CRYSTAL", Address = mockAddress, Amount = 100 });
var serviceProvider = TestServices.CreateServices(balanceRepositoryMock: mockRepo);
var query = $$"""
query {
balance(currencyTicker: "CRYSTAL", address: "{{mockAddress}}")
}
""";
var result = await TestServices.ExecuteRequestAsync(serviceProvider, b => b.SetQuery(query));
await Verify(result);
}
Add Test for NCG Query
[Fact]
public async Task GraphQL_Query_Balance_NCG_Returns_CorrectValue()
{
var mockAddress = new Address("0x0000000000000000000000000000000000000000");
var mockRepo = new Mock<IBalanceRepository>();
mockRepo
.Setup(repo => repo.GetByAddressAsync("NCG".ToCurrency(), mockAddress))
.ReturnsAsync(new Balance { Currency = "NCG", Address = mockAddress, Amount = 200 });
var serviceProvider = TestServices.CreateServices(balanceRepositoryMock: mockRepo);
var query = $$"""
query {
balance(currencyTicker: "NCG", address: "{{mockAddress}}")
}
""";
var result = await TestServices.ExecuteRequestAsync(serviceProvider, b => b.SetQuery(query));
await Verify(result);
}
Add Test for Missing Inputs (Exception Handling)
[Fact]
public async Task GraphQL_Query_Balance_Throws_When_No_Inputs_Provided()
{
var mockRepo = new Mock<IBalanceRepository>();
var serviceProvider = TestServices.CreateServices(balanceRepositoryMock: mockRepo);
var query = $$"""
query {
balance(address: "0x0000000000000000000000000000000000000000")
}
""";
var exception = await Assert.ThrowsAsync<GraphQLRequestException>(
async () => await TestServices.ExecuteRequestAsync(serviceProvider, b => b.SetQuery(query))
);
Assert.Contains("Either currency or currencyTicker must be provided.", exception.Message);
}
Notes
Use Verify:
The Verify method ensures that the returned GraphQL response matches the expected snapshot, simplifying validation of nested or complex structures.
Scenarios Tested:
Query with currencyTicker = "CRYSTAL".
Query with currencyTicker = "NCG".
Exception when neither currency nor currencyTicker is provided.
Mocking:
The repository is mocked to isolate the test from dependencies, ensuring it focuses solely on the query's behavior.
Additional Input (CurrencyInput):
Tests for CurrencyInput are excluded as per your requirements but can be added later.
We need to add unit tests for the
Balance
query to ensure it works as expected for differentcurrencyTicker
values and properly handles missing inputs.Create
BalanceQueryTest.cs
with Initial integration TestsMimir.Tests/QueryTests/BalanceQueryTest.cs
Mocking
IServiceProvider
with MockBalanceRepository
Create
IBalanceRepository
Interface:IBalanceRepository
interface and apply it toBalanceRepository
.Make
BalanceRepository.GetByAddressAsync
Virtual:GetByAddressAsync
method to bevirtual
to allow mocking.Mock the Repository for Different Scenarios:
Add Test for
NCG
QueryAdd Test for Missing Inputs (Exception Handling)
Notes
Use
Verify
:Verify
method ensures that the returned GraphQL response matches the expected snapshot, simplifying validation of nested or complex structures.Scenarios Tested:
currencyTicker = "CRYSTAL"
.currencyTicker = "NCG"
.currency
norcurrencyTicker
is provided.Mocking:
Additional Input (
CurrencyInput
):CurrencyInput
are excluded as per your requirements but can be added later.