plam
plam
CC#
Created by plam on 3/8/2023 in #help
❔ Mocking EF Core extensions methods.
yeah that's what I literally mentioned
11 replies
CC#
Created by plam on 3/8/2023 in #help
❔ Mocking EF Core extensions methods.
Pretty tough question, I guess I will just use an in-memory database testing method for all my repositories.
11 replies
CC#
Created by plam on 3/8/2023 in #help
❔ Mocking EF Core extensions methods.
Here I am not mocking any of extension methods, and due to this reason the test fails and returns null. Basically, need to mock ContainsAsync here, and the test will succeed.
[Fact]
public async Task UpdateAsync_ReturnsUpdatedOrderItem_WhenOrderItemExists()
{
// Arrange
var orderItems = new List<OrderItem>
{
new OrderItem {Id = "1", Title = "Item 1", Description = "Description 1", Price = 9.99f, Quantity = 2},
new OrderItem {Id = "2", Title = "Item 2", Description = "Description 2", Price = 4.99f, Quantity = 1}
};

var mockDbSet = orderItems.AsQueryable().BuildMockDbSet();
_mockDbContext.Setup(x => x.OrderItems).Returns(mockDbSet.Object);

var updatedOrderItem = new OrderItem
{
Id = "2",
Title = "Updated Item 2",
Description = "Updated Description 2",
Price = 19.99f,
Quantity = 3
};

// Act
var result = await _orderItemRepository.UpdateAsync(updatedOrderItem);

// Assert
Assert.NotNull(result);
Assert.Equal(updatedOrderItem, result);
}
[Fact]
public async Task UpdateAsync_ReturnsUpdatedOrderItem_WhenOrderItemExists()
{
// Arrange
var orderItems = new List<OrderItem>
{
new OrderItem {Id = "1", Title = "Item 1", Description = "Description 1", Price = 9.99f, Quantity = 2},
new OrderItem {Id = "2", Title = "Item 2", Description = "Description 2", Price = 4.99f, Quantity = 1}
};

var mockDbSet = orderItems.AsQueryable().BuildMockDbSet();
_mockDbContext.Setup(x => x.OrderItems).Returns(mockDbSet.Object);

var updatedOrderItem = new OrderItem
{
Id = "2",
Title = "Updated Item 2",
Description = "Updated Description 2",
Price = 19.99f,
Quantity = 3
};

// Act
var result = await _orderItemRepository.UpdateAsync(updatedOrderItem);

// Assert
Assert.NotNull(result);
Assert.Equal(updatedOrderItem, result);
}
11 replies
CC#
Created by plam on 3/8/2023 in #help
❔ Mocking EF Core extensions methods.
yeah, I have been looking through this one
11 replies
CC#
Created by plam on 3/8/2023 in #help
❔ Mocking EF Core extensions methods.
I have also tried out using AnyAsync in order to mock ContainsAsync in my DbSet setup, but it's also an extension method:
[Fact]
public async Task UpdateAsync_ReturnsUpdatedOrderItem_WhenOrderItemExists()
{
// Arrange
var orderItems = new List<OrderItem>
{
new OrderItem {Id = "1", Title = "Item 1", Description = "Description 1", Price = 9.99f, Quantity = 2},
new OrderItem {Id = "2", Title = "Item 2", Description = "Description 2", Price = 4.99f, Quantity = 1}
};

var mockDbContext = new Mock<ApplicationDbContext>(new DbContextOptions<ApplicationDbContext>());
var mockDbSet = orderItems.AsQueryable().BuildMockDbSet();

mockDbSet.Setup(x => x.AnyAsync(It.IsAny<Expression<Func<OrderItem, bool>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync((Expression<Func<OrderItem, bool>> predicate, CancellationToken cancellationToken)
=> orderItems.AsQueryable().Any(predicate.Compile()));


mockDbContext.Setup(x => x.OrderItems).Returns(mockDbSet.Object);

var updatedOrderItem = new OrderItem
{
Id = "2",
Title = "Updated Item 2",
Description = "Updated Description 2",
Price = 19.99f,
Quantity = 3
};

// Act
var result = await _orderItemRepository.UpdateAsync(updatedOrderItem);

// Assert
Assert.NotNull(result);
Assert.Equal(updatedOrderItem, result);

mockDbSet.Verify(x => x.Update(updatedOrderItem), Times.Once());
mockDbContext.Verify(x => x.SaveChangesAsync(default), Times.Once());
}
[Fact]
public async Task UpdateAsync_ReturnsUpdatedOrderItem_WhenOrderItemExists()
{
// Arrange
var orderItems = new List<OrderItem>
{
new OrderItem {Id = "1", Title = "Item 1", Description = "Description 1", Price = 9.99f, Quantity = 2},
new OrderItem {Id = "2", Title = "Item 2", Description = "Description 2", Price = 4.99f, Quantity = 1}
};

var mockDbContext = new Mock<ApplicationDbContext>(new DbContextOptions<ApplicationDbContext>());
var mockDbSet = orderItems.AsQueryable().BuildMockDbSet();

mockDbSet.Setup(x => x.AnyAsync(It.IsAny<Expression<Func<OrderItem, bool>>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync((Expression<Func<OrderItem, bool>> predicate, CancellationToken cancellationToken)
=> orderItems.AsQueryable().Any(predicate.Compile()));


mockDbContext.Setup(x => x.OrderItems).Returns(mockDbSet.Object);

var updatedOrderItem = new OrderItem
{
Id = "2",
Title = "Updated Item 2",
Description = "Updated Description 2",
Price = 19.99f,
Quantity = 3
};

// Act
var result = await _orderItemRepository.UpdateAsync(updatedOrderItem);

// Assert
Assert.NotNull(result);
Assert.Equal(updatedOrderItem, result);

mockDbSet.Verify(x => x.Update(updatedOrderItem), Times.Once());
mockDbContext.Verify(x => x.SaveChangesAsync(default), Times.Once());
}
11 replies
CC#
Created by ghoulam on 11/13/2022 in #help
❔ Where can i learn about APIs and how to use Postman properly ?
Check out the MSDN documentation on the topic of APIs. As for the Postman, I do recommend trying it by yourself while reading their "Getting started" section: https://learning.postman.com/docs/getting-started/introduction/
3 replies
CC#
Created by Anton on 11/12/2022 in #help
❔ ASP.NET Core how to let a controller be mapped only in development mode
Why don't you just return NotFound as for the controller which is flagged with attribute for DevOnly or sum
37 replies
CC#
Created by CrosRoad95 on 11/12/2022 in #help
❔ check if string is valid guid without guid.TryParse
ikr?
10 replies
CC#
Created by Anton on 11/12/2022 in #help
❔ Winforms rotated text in `ToolStripButton` appears bold
8 replies
CC#
Created by Anton on 11/12/2022 in #help
❔ Winforms rotated text in `ToolStripButton` appears bold
8 replies
CC#
Created by CrosRoad95 on 11/12/2022 in #help
❔ check if string is valid guid without guid.TryParse
TryParse would be great here, but as you've mentioned to avoid it, here's what you can actually do:
public static class Example {
public static bool TryParseGuid(string guidString, out Guid guid)
{
if (guidString == null) throw new ArgumentNullException("guidString");
try
{
guid = new Guid(guidString);
return true;
}
catch (FormatException)
{
guid = default(Guid);
return false;
}
}
}
public static class Example {
public static bool TryParseGuid(string guidString, out Guid guid)
{
if (guidString == null) throw new ArgumentNullException("guidString");
try
{
guid = new Guid(guidString);
return true;
}
catch (FormatException)
{
guid = default(Guid);
return false;
}
}
}
10 replies
CC#
Created by Anton on 11/12/2022 in #help
❔ Winforms rotated text in `ToolStripButton` appears bold
void tooltip_Popup(object sender, PopupEventArgs e)
{
e.ToolTipSize = TextRenderer.MeasureText(toolTipText, new Font("Courier New", 10.0f, FontStyle.Bold )); // !
e.ToolTipSize = new Size(e.ToolTipSize.Width + TOOLTIP_XOFFSET, e.ToolTipSize.Height + TOOLTIP_YOFFSET);
}
void tooltip_Popup(object sender, PopupEventArgs e)
{
e.ToolTipSize = TextRenderer.MeasureText(toolTipText, new Font("Courier New", 10.0f, FontStyle.Bold )); // !
e.ToolTipSize = new Size(e.ToolTipSize.Width + TOOLTIP_XOFFSET, e.ToolTipSize.Height + TOOLTIP_YOFFSET);
}
8 replies
CC#
Created by Anton on 11/12/2022 in #help
❔ ASP.NET Core how to let a controller be mapped only in development mode
It depends
37 replies
CC#
Created by Anton on 11/12/2022 in #help
❔ ASP.NET Core how to let a controller be mapped only in development mode
There's an answer with an implementation of a filter, go for it
37 replies
CC#
Created by Anton on 11/12/2022 in #help
❔ ASP.NET Core how to let a controller be mapped only in development mode
37 replies
CC#
Created by Anton on 11/12/2022 in #help
❔ ASP.NET Core how to let a controller be mapped only in development mode
After the successful injection of the interface you're pretty much able to check for the environment inside of the body of action.
public class ExampleController : Controller
{
private readonly IHostEnvironment _hostEnvironment;

public ExampleController(IHostEnvironment hostEnvironment)
{
_hostEnvironment = hostEnvironment;
}

public IActionResult View()
{
if (!_hostEnvironment.IsDevelopment()) // Return something else for Development.
}
}
public class ExampleController : Controller
{
private readonly IHostEnvironment _hostEnvironment;

public ExampleController(IHostEnvironment hostEnvironment)
{
_hostEnvironment = hostEnvironment;
}

public IActionResult View()
{
if (!_hostEnvironment.IsDevelopment()) // Return something else for Development.
}
}
37 replies
CC#
Created by Ash_ on 11/12/2022 in #help
❔ How do i sort a list of objects by multiple possible items
gl
40 replies
CC#
Created by Ash_ on 11/12/2022 in #help
❔ How do i sort a list of objects by multiple possible items
Make sure to check out more documentation on this method, specifically just to avoid the myriad of duplicate code.
40 replies