bribri
bribri
CC#
Created by bribri on 2/21/2025 in #help
integration testing my moqs are not working
I have made this code. The current account service gets the user account id with logged in/api key .... But for some reason when i moq it just gives null
public class AddArticleToFavoritesTests
: IClassFixture<WebApplicationFactory<Program>>
{
private readonly WebApplicationFactory<Program> _factory;
private readonly Mock<ICurrentAccountService> _mock;

public AddArticleToFavoritesTests(WebApplicationFactory<Program> factory)
{
_factory = factory;
_mock = new Mock<ICurrentAccountService>();
}

[Fact]
public async Task AddArticleToFavoritesShouldReturnSucces()
{

_mock.Setup(x => x.GetCurrentAccountIdAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(1);

var client = _factory.CreateClient();

var requestBody = new { articleId = 1138 };
var content = new StringContent(JsonSerializer.Serialize(requestBody), Encoding.UTF8, "application/json");

var response = await client.PostAsync("/api/Articles/AddToFavorites", content);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
public class AddArticleToFavoritesTests
: IClassFixture<WebApplicationFactory<Program>>
{
private readonly WebApplicationFactory<Program> _factory;
private readonly Mock<ICurrentAccountService> _mock;

public AddArticleToFavoritesTests(WebApplicationFactory<Program> factory)
{
_factory = factory;
_mock = new Mock<ICurrentAccountService>();
}

[Fact]
public async Task AddArticleToFavoritesShouldReturnSucces()
{

_mock.Setup(x => x.GetCurrentAccountIdAsync(It.IsAny<CancellationToken>()))
.ReturnsAsync(1);

var client = _factory.CreateClient();

var requestBody = new { articleId = 1138 };
var content = new StringContent(JsonSerializer.Serialize(requestBody), Encoding.UTF8, "application/json");

var response = await client.PostAsync("/api/Articles/AddToFavorites", content);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
In the query handler
var accountId = await currentAccountService.GetCurrentAccountIdAsync(cancellationToken);
var accountId = await currentAccountService.GetCurrentAccountIdAsync(cancellationToken);
the iCurrentAccountservice
Task<int?> GetCurrentAccountIdAsync(CancellationToken cancellationToken);
Task<int?> GetCurrentAccountIdAsync(CancellationToken cancellationToken);
the currentaccount service is a Singleton with Di and i'm using clean architecture. I tried alot but I cant get GetCurrentAccountIdAsync to return an id
7 replies
CC#
Created by bribri on 12/13/2023 in #help
Ef insert many to many
How do I insert an new object to my database? right now objects are like this:
c#
class order{
int id
List<product>
}
c#
class order{
int id
List<product>
}
c#
class orderproduct{
int orderid
int productId
}
c#
class orderproduct{
int orderid
int productId
}
c#
class Product{
int id
List<order>}
c#
class Product{
int id
List<order>}
so I want to add an order object with an list of products(that contain the id) but I get erros. when I just use 1 of the same product: Cannot insert explicit value for identity column in table 'Products' when IDENTITY_INSERT is set to OFF. when my list contains the same product twice: 'The instance of entity type 'Product' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. Not really sure how to fix it.... Can also use dapper if that's easier
2 replies
CC#
Created by bribri on 6/24/2023 in #help
✅ Problems with wrapping in wpf Listbox
8 replies
CC#
Created by bribri on 6/21/2023 in #help
❔ Probably easy question about relative paths
Is there a way to let my relative path start at my solution folder? right now i do ..\..\..\..\filesINeed to get out of my project folder to go to another folder Would it no be easier to have something like startatsolution\filesINeed So I just start in the lowest folder to go to any other project etc
12 replies
CC#
Created by bribri on 6/20/2023 in #help
start an python script from an c# project
24 replies
CC#
Created by bribri on 6/19/2023 in #help
❔ WPF application resources not found in window
18 replies