public void Close_Ad_As_Non_Owner_Should_Return_400_Bad_Request() { var fakeAd = this.mocks.AdRepositoryMock.Object.All().FirstOrDefault(a => a.Status == AdStatus.Open); if (fakeAd == null) { Assert.Fail("Cannot perform test - no ad available."); } var mockContext = new Mock<IOnlineShopData>(); mockContext.Setup(c => c.Ads) .Returns(this.mocks.AdRepositoryMock.Object); var fakeUser = this.mocks.UserRepositoryMock.Object.All().FirstOrDefault(u => u.Id != fakeAd.OwnerId); if (fakeUser == null) { Assert.Fail("Cannot perform test - no user available."); } var mockIdProvider = new Mock<IUserIdProvider>(); mockIdProvider.Setup(ip => ip.GetUserId()) .Returns(fakeUser.Id); var adsController = new AdsController(mockContext.Object, mockIdProvider.Object); this.SetupController(adsController); var response = adsController.CloseAd(fakeAd.Id).ExecuteAsync(CancellationToken.None).Result; Assert.AreEqual(HttpStatusCode.BadRequest, response.StatusCode); mockContext.Verify(c => c.SaveChanges(), Times.Never); Assert.AreEqual(AdStatus.Open, fakeAd.Status); }
public void Closing_Ads_As_Non_Owner_Should_Return_400BadRequest() { var fakeAd = this._mocks.AdRepoMock.Object.All() .FirstOrDefault(ad => ad.Status == AdStatus.Open); if (fakeAd == null) Assert.Fail("No ads available to perform the test."); var adId = fakeAd.Id; var mockContext = new Mock<IOnlineShopData>(); mockContext.Setup(c => c.Ads) .Returns(this._mocks.AdRepoMock.Object); var mockIdProvider = new Mock<IUserIdProvider>(); mockIdProvider.Setup(ip => ip.GetUserId()) .Returns("Ivan4o"); var adsController = new AdsController(mockContext.Object, mockIdProvider.Object); this.SetupController(adsController); var response = adsController.CloseAd(adId) .ExecuteAsync(CancellationToken.None).Result; Assert.AreEqual(response.StatusCode, HttpStatusCode.Unauthorized); mockContext.Verify(c => c.SaveChanges(), Times.Never); Assert.AreEqual(_mocks.AdRepoMock.Object.All().FirstOrDefault(ad => ad.Id == adId).Status, AdStatus.Open); }