Quantcast
Channel: What's the difference between a mock & stub? - Stack Overflow
Viewing all articles
Browse latest Browse all 43

Answer by Adarsh Shah for What's the difference between a mock & stub?

$
0
0

See below example of mocks vs stubs using C# and Moq framework. Moq doesn't have a special keyword for Stub but you can use Mock object to create stubs too.

namespace UnitTestProject2{    using Microsoft.VisualStudio.TestTools.UnitTesting;    using Moq;    [TestClass]    public class UnitTest1    {        /// <summary>        /// Test using Mock to Verify that GetNameWithPrefix method calls Repository GetName method "once" when Id is greater than Zero        /// </summary>        [TestMethod]        public void GetNameWithPrefix_IdIsTwelve_GetNameCalledOnce()        {            // Arrange             var mockEntityRepository = new Mock<IEntityRepository>();            mockEntityRepository.Setup(m => m.GetName(It.IsAny<int>()));            var entity = new EntityClass(mockEntityRepository.Object);            // Act             var name = entity.GetNameWithPrefix(12);            // Assert            mockEntityRepository.Verify(m => m.GetName(It.IsAny<int>()), Times.Once);        }        /// <summary>        /// Test using Mock to Verify that GetNameWithPrefix method doesn't call Repository GetName method when Id is Zero        /// </summary>        [TestMethod]        public void GetNameWithPrefix_IdIsZero_GetNameNeverCalled()        {            // Arrange             var mockEntityRepository = new Mock<IEntityRepository>();            mockEntityRepository.Setup(m => m.GetName(It.IsAny<int>()));            var entity = new EntityClass(mockEntityRepository.Object);            // Act             var name = entity.GetNameWithPrefix(0);            // Assert            mockEntityRepository.Verify(m => m.GetName(It.IsAny<int>()), Times.Never);        }        /// <summary>        /// Test using Stub to Verify that GetNameWithPrefix method returns Name with a Prefix        /// </summary>        [TestMethod]        public void GetNameWithPrefix_IdIsTwelve_ReturnsNameWithPrefix()        {            // Arrange             var stubEntityRepository = new Mock<IEntityRepository>();            stubEntityRepository.Setup(m => m.GetName(It.IsAny<int>()))                .Returns("Stub");            const string EXPECTED_NAME_WITH_PREFIX = "Mr. Stub";            var entity = new EntityClass(stubEntityRepository.Object);            // Act             var name = entity.GetNameWithPrefix(12);            // Assert            Assert.AreEqual(EXPECTED_NAME_WITH_PREFIX, name);        }    }    public class EntityClass    {        private IEntityRepository _entityRepository;        public EntityClass(IEntityRepository entityRepository)        {            this._entityRepository = entityRepository;        }        public string Name { get; set; }        public string GetNameWithPrefix(int id)        {            string name = string.Empty;            if (id > 0)            {                name = this._entityRepository.GetName(id);            }            return "Mr. "+ name;        }    }    public interface IEntityRepository    {        string GetName(int id);    }    public class EntityRepository:IEntityRepository    {        public string GetName(int id)        {            // Code to connect to DB and get name based on Id            return "NameFromDb";        }    }}

Viewing all articles
Browse latest Browse all 43

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>