<commit_before>﻿using System;

using Moq;
using NUnit.Framework;

using WhenItsDone.Data.Contracts;
using WhenItsDone.Data.Repositories;
using WhenItsDone.Data.Tests.Mocks;
using System.Data.Entity;
using System.Reflection;
using WhenItsDone.Models.Contracts;

namespace WhenItsDone.Data.Tests.RepositoriesTests.AsyncGenericRepositoryTests
{
    [TestFixture]
    public class Constructor_Should
    {
        [Test]
        public void ShouldThrowArgumentNullExceptionWithCorrectMessage_WhenIWhenItsDoneDbContextParameterIsNull()
        {
            IWhenItsDoneDbContext invalidDbContext = null;

            Assert.That(
                () => new AsyncGenericRepository<IDbModel>(invalidDbContext),
                Throws.InstanceOf<ArgumentNullException>().With.Message.Contains("DbContext"));
        }

        [Test]
        public void ShouldInvoke_DbContextSetMethodOnce()
        {
            var stubModel = new Mock<IDbModel>();

            var dbSetConstructor = typeof(DbSet<IDbModel>).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null);
            var dbSet = (DbSet<IDbModel>)dbSetConstructor.Invoke(null);

            var mockDbContext = new Mock<IWhenItsDoneDbContext>();
            mockDbContext.Setup(mock => mock.Set<IDbModel>()).Returns(dbSet);

            var asyncGenericRepository = new AsyncGenericRepository<IDbModel>(mockDbContext.Object);

            mockDbContext.Verify(mock => mock.Set<IDbModel>(), Times.Once);
        }

        [Test]
        public void ShouldThrowArgumentExceptionWithCorrectMessage_WhenDbContextDoesNotContainADbSetOfTheCorrectType()
        {
            var mockDbContext = new Mock<IWhenItsDoneDbContext>();
            mockDbContext.Setup(mock => mock.Set<IDbModel>()).Returns<DbSet<IDbModel>>(null);

            Assert.That(
                () => new AsyncGenericRepository<IDbModel>(mockDbContext.Object),
                Throws.InstanceOf<ArgumentException>().With.Message.Contains("DbContext does not contain DbSet"));
        }
    }
}<commit_msg>fixed usings<commit_after>﻿using System;
using System.Data.Entity;
using System.Reflection;

using Moq;
using NUnit.Framework;

using WhenItsDone.Data.Contracts;
using WhenItsDone.Data.Repositories;
using WhenItsDone.Models.Contracts;

namespace WhenItsDone.Data.Tests.RepositoriesTests.AsyncGenericRepositoryTests
{
    [TestFixture]
    public class Constructor_Should
    {
        [Test]
        public void ShouldThrowArgumentNullExceptionWithCorrectMessage_WhenIWhenItsDoneDbContextParameterIsNull()
        {
            IWhenItsDoneDbContext invalidDbContext = null;

            Assert.That(
                () => new AsyncGenericRepository<IDbModel>(invalidDbContext),
                Throws.InstanceOf<ArgumentNullException>().With.Message.Contains("DbContext"));
        }

        [Test]
        public void ShouldInvoke_DbContextSetMethodOnce()
        {
            var dbSetConstructor = typeof(DbSet<IDbModel>).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[0], null);
            var dbSet = (DbSet<IDbModel>)dbSetConstructor.Invoke(null);

            // Return type of DbContext.Set<>() method is DbSet, rather than IDbSet
            // mocking IDbSet does not work.
            //var dbSet = new Mock<IDbSet<IDbModel>>();

            var mockDbContext = new Mock<IWhenItsDoneDbContext>();
            mockDbContext.Setup(mock => mock.Set<IDbModel>()).Returns(dbSet);

            var asyncGenericRepository = new AsyncGenericRepository<IDbModel>(mockDbContext.Object);

            mockDbContext.Verify(mock => mock.Set<IDbModel>(), Times.Once);
        }

        [Test]
        public void ShouldThrowArgumentExceptionWithCorrectMessage_WhenDbContextDoesNotContainADbSetOfTheCorrectType()
        {
            var mockDbContext = new Mock<IWhenItsDoneDbContext>();
            mockDbContext.Setup(mock => mock.Set<IDbModel>()).Returns<DbSet<IDbModel>>(null);

            Assert.That(
                () => new AsyncGenericRepository<IDbModel>(mockDbContext.Object),
                Throws.InstanceOf<ArgumentException>().With.Message.Contains("DbContext does not contain DbSet"));
        }
    }
}
{'fraction_non_alphanumeric': 0.09895586141433317, 'fraction_numerical': 0.00047460844803037496, 'mean_word_length': 3.1732673267326734, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2684978', 'n_tokens_mistral': 1177, 'n_tokens_neox': 1119, 'n_words': 184}