Instantly share code, notes, and snippets.

View SetupAllProperties.cs
[Fact]
public void TrackAllPropertiesWithSetupAllProperties()
{
var mock = new Mock<IPropertyManager>();
//mock.SetupProperty(m => m.FirstName);
//Comment this and uncomment SetupProperty, the assertion fails
mock.SetupAllProperties();
View Verify.cs
[Fact]
public void Verify()
{
var mock = new Mock<IPropertyManager>();
var nameUser = new PropertyManagerConsumer(mock.Object);
nameUser.ChangeRemoteName("My dear old wig");
//we are verifying that ChangeRemoteName sends the correct string to MutateFirstName
mock.Verify(m => m.MutateFirstName(It.Is<string>(a => a == "My dear old wig")), Times.Once);
View GetStockSelectListItemsWithOptionGroup.cs
public List<SelectListItem> GetStockSelectListItemsWithOptionGroup(Company[] companies, Stock[] stocks)
{
var optionGroups = companies.GroupBy(c => c.CompanyId, c => c.CompanyName).Select(c => new SelectListGroup { Name = c.First() });
return stocks.Select(s => new SelectListItem
{
Text = s.StockName,
Value = s.StockId.ToString(),
Group = optionGroups.Single(o => o.Name.Trim() == s.CompanyName.Trim())
}).ToList();
View LinqSamenessTest.cs
public class Quote
{
public string Word { get; set; }
}
[Fact]
void LinqSingleMethod_WhenLazilyEvaluated_ReturnsDifferentInstance()
{
var quoteWords = new[]
{
View Mock.cs
[Fact]
public async Task DetailPassTheCorrectIdIntoPortfolioByIdQuery()
{
//This is a mock
var _mediator = new Mock<IMediator>() ;
var _mapper = new Mock<IMapper>();
//Arrange
var sut = new PortfolioController(_mapper.Object, _mediator.Object);
View IsolationFrameworksComparison.cs
//Moq
var lollipop = new Mock<ICandy>();
var shop = new Mock<ICandyShop>();
shop.Setup(s => s.GetTopSellingCandy()).Returns(lollipop);
shop.Verify(s => s.BuyCandy(lollipop), Times.AtMostOnce());
//NSubstitude
var lollipop = Substitute.For<ICandy>();
var shop = Substitute.For<ICandyShop>();
shop.GetTopSellingCandy().Returns(lollipop);
View Startup.cs
public class Startup
{
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
Configuration = builder.Build();
}
View ProductApiController.cs
[Produces("application/json")]
[Route("api/Product")]
public class ProductApiController : Controller
{
public OkObjectResult Get()
{
var products = new { Products = new[] { new { Id = 1, Name = "Normal Bear" }, new { Id = 2, Name = "Panda Bearer" }, new { Id = 3, Name = "JWT Bearer" } } };
return Ok(products);
}
View SecureProductApi.cs
[Produces("application/json")]
[Route("api/Product")]
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Policy = "Trusted")]
public class ProductApiController : Controller
{
public OkObjectResult Get()
{
var products = new { Products = new[] { new { Id = 1, Name = "Normal Bear" }, new { Id = 2, Name = "Panda Bearer" }, new { Id = 3, Name = "JWT Bearer" } } };
return Ok(products);
View Generate.cs
[AllowAnonymous]
[HttpPost]
public async Task<IActionResult> Generate([FromBody] LoginViewModel model)
{
if (!ModelState.IsValid) return BadRequest("Could not create token");
var user = await _userManager.FindByEmailAsync(model.Email);
if (user == null) return BadRequest("Could not create token");