Last active
December 13, 2020 20:52
-
-
Save bjornmicallef/df1e21b6eb6e23d9ddbbc9f84f8f5497 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserSettingsServiceTests | |
{ | |
private readonly DbContextOptions<StockNotificationContext> _options; | |
private readonly StockNotificationContext _dbContext; | |
private readonly UserSettingsService _userSettingsService; | |
private const string _username = "UnitTestUsername"; | |
public UserSettingsServiceTests() | |
{ | |
_options = new DbContextOptionsBuilder<StockNotificationContext>().UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options; | |
_dbContext = new StockNotificationContext(_options); | |
_userSettingsService = new UserSettingsService(_dbContext); | |
} | |
[Fact] | |
public async Task UpdateUserSellerPreferences_Updates_User_Settings() | |
{ | |
// Arrange | |
await _dbContext.Database.EnsureDeletedAsync(); | |
var listOfSellers = new List<tblSellers>(); | |
listOfSellers.Add(new tblSellersBuilder().WithId(1).WithName("Amazon").WithUrl("https://www.amazon.co.uk").Build()); | |
listOfSellers.Add(new tblSellersBuilder().WithId(2).WithName("Ebay").WithUrl("https://www.ebay.co.uk").Build()); | |
// other sellers added here | |
_dbContext.tblSellers.AddRange(listOfSellers); | |
var listOfExclSellers = new List<tblExcludedSellers>(); | |
listOfExclSellers.Add(new tblExcludedSellersBuilder().WithUsername(_username).WithSellerId(5).Build()); | |
listOfExclSellers.Add(new tblExcludedSellersBuilder().WithUsername(_username).WithSellerId(6).Build()); | |
_dbContext.tblExcludedSellers.AddRange(listOfExclSellers); | |
await _dbContext.SaveChangesAsync(); | |
var newListOfSellers = new List<int>() { 1, 2, 3, 5 }; | |
// Act | |
var task = _userSettingsService.UpdateUserSellerPreferences(_username, newListOfSellers); | |
await task; | |
// Assert | |
var updatedList = _dbContext.tblExcludedSellers.Where(x => x.Username == _username).Select(x => x.SellerId).ToList(); | |
Assert.Equal(2, updatedList.Count); | |
Assert.Contains(4, updatedList); | |
Assert.Contains(6, updatedList); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment