Skip to content

Commit

Permalink
Fix MusicStore tests
Browse files Browse the repository at this point in the history
Fixes #12097

The issue here is that the tests were incorrectly creating multiple instances of the same entity. This was being masked by a bug in EF which has just been fixed. The fix here is to not try to track multiple instances with the same ID.
  • Loading branch information
ajcvickers committed Jul 11, 2019
1 parent b395c2e commit f16467f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
6 changes: 1 addition & 5 deletions src/MusicStore/samples/MusicStore/Models/Album.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,9 @@ public class Album
[Required]
public DateTime Created { get; set; }

/// <summary>
/// TODO: Temporary hack to populate the orderdetails until EF does this automatically.
/// </summary>
public Album()
{
OrderDetails = new List<OrderDetail>();
Created = DateTime.UtcNow;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ public class MusicStoreContext : IdentityDbContext<ApplicationUser>
public MusicStoreContext(DbContextOptions<MusicStoreContext> options)
: base(options)
{
// TODO: #639
//ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
}

public DbSet<Album> Albums { get; set; }
Expand All @@ -22,4 +20,4 @@ public MusicStoreContext(DbContextOptions<MusicStoreContext> options)
public DbSet<CartItem> CartItems { get; set; }
public DbSet<OrderDetail> OrderDetails { get; set; }
}
}
}
34 changes: 21 additions & 13 deletions src/MusicStore/test/MusicStore.Test/ShoppingCartControllerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,16 @@ public async Task AddToCart_AddsItemToCart()

// Creates the albums of AlbumId = 1 ~ 10.
var dbContext = _fixture.Context;
var albums = CreateTestAlbums(itemPrice: 10);
var albums = CreateTestAlbums(
10,
new Artist
{
ArtistId = 1, Name = "Kung Fu Kenny"
}, new Genre
{
GenreId = 1, Name = "Rap"
});

dbContext.AddRange(albums);
dbContext.SaveChanges();

Expand Down Expand Up @@ -204,7 +213,14 @@ public async Task RemoveFromCart_RemovesItemFromCart()

private static CartItem[] CreateTestCartItems(string cartId, decimal itemPrice, int numberOfItem)
{
var albums = CreateTestAlbums(itemPrice);
var albums = CreateTestAlbums(
itemPrice, new Artist
{
ArtistId = 1, Name = "Kung Fu Kenny"
}, new Genre
{
GenreId = 1, Name = "Rap"
});

var cartItems = Enumerable.Range(1, numberOfItem).Select(n =>
new CartItem()
Expand All @@ -218,24 +234,16 @@ private static CartItem[] CreateTestCartItems(string cartId, decimal itemPrice,
return cartItems;
}

private static Album[] CreateTestAlbums(decimal itemPrice)
private static Album[] CreateTestAlbums(decimal itemPrice, Artist artist, Genre genre)
{
return Enumerable.Range(1, 10).Select(n =>
new Album
{
Title = "Greatest Hits",
AlbumId = n,
Price = itemPrice,
Artist = new Artist
{
ArtistId = 1,
Name = "Kung Fu Kenny"
},
Genre = new Genre
{
GenreId = 1,
Name = "Rap"
}
Artist = artist,
Genre = genre
}).ToArray();
}

Expand Down

0 comments on commit f16467f

Please sign in to comment.