Showing posts with label NUnit. Show all posts
Showing posts with label NUnit. Show all posts

Friday, 8 November 2013

Mock return value only once

Processing records using mock and NUnit


 public List ProcessRecords(IRecordsReader conf)
        {
            var pageHitsResults = new List();
            if (conf != null && conf.HasRecords)
                while (conf.Read())
                {
                    pageHitsResults.Add(
                        new PageHitsResults { Date = conf.GetDateTime("Date"), Hits = conf.GetInt("Hits"), TotalHits = conf.GetInt("TotalHits") });
                } return pageHitsResults;
        }
        [Test]
        public void ProcessRecordsTests()
        {
            var isqlHelperMock = new Mock();
            var iRecordsReaderMock = new Mock();
            iRecordsReaderMock.Setup(x => x.HasRecords).Returns(true);
            int i = 1;
            iRecordsReaderMock.Setup(x => x.Read()).Returns(() => { return i++ == 1; }); var dateTime = DateTime.Now; iRecordsReaderMock.Setup(x => x.GetDateTime("Date")).Returns(dateTime); iRecordsReaderMock.Setup(x => x.GetInt("Hits")).Returns(55); iRecordsReaderMock.Setup(x => x.GetInt("TotalHits")).Returns(123); var pageHitsLogic = new PageHitsLogic(isqlHelperMock.Object); var actual = pageHitsLogic.ProcessRecords(iRecordsReaderMock.Object); Assert.AreEqual(1, actual.Count); Assert.AreEqual(dateTime, actual.First().Date); Assert.AreEqual(55, actual.First().Hits); Assert.AreEqual(123, actual.First().TotalHits);
        }
        [Test]
        public void ProcessRecordsTestsTest()
        {
            var isqlHelperMock = new Mock(); var iRecordsReaderMock = new Mock(); iRecordsReaderMock.Setup(x => x.HasRecords).Returns(true); int i = 1; iRecordsReaderMock.Setup(x => x.Read()).Returns().Returns(() => i++ == 1); var dateTime = DateTime.Now; iRecordsReaderMock.Setup(x => x.GetDateTime("Date")).Returns(dateTime); iRecordsReaderMock.Setup(x => x.GetInt("Hits")).Returns(55); iRecordsReaderMock.Setup(x => x.GetInt("TotalHits")).Returns(123); var pageHitsLogic = new PageHitsLogic(isqlHelperMock.Object); i++;
            // if used here the defalut value of I is 2 and the function for reading does did not yet execued 
            var actual = pageHitsLogic.ProcessRecords(iRecordsReaderMock.Object);
            Assert.AreEqual(1, actual.Count);
            Assert.AreEqual(dateTime, actual.First().Date);
            Assert.AreEqual(55, actual.First().Hits);
            Assert.AreEqual(123, actual.First().TotalHits);
        }

Friday, 16 August 2013

NUnit using data source

I am using NUnit to run unit test.
I have been using  test cases for a while but I needed to use multiple tests with date time.
I did not wanted to write a parsing string into date time.
My code generates html as you can see from code below.


My solution after while of reading documentation:








 [Test, TestCaseSource(typeof(NewsHtmlHelperTests), "DisplayRelatedUpdateLinkTestSources")]
        public void DisplayRelatedUpdateLinkTests(string sourcePath, string articlePath, string name, DateTime createdDate, MvcHtmlString expected)
        {
            var actual = NewsHelper.DisplayRelatedUpdateLink(null, sourcePath, articlePath, name, createdDate);
            Assert.AreEqual(expected.ToString(),actual.ToString());
        }

        public static IEnumerable DisplayRelatedUpdateLinkTestSources
        {
            get
            {
                yield return new TestCaseData("/test", "/test", "Test Article", new DateTime(2012, 05, 28), 
                                            new MvcHtmlString("
  • 28 May Test Article
  • ")); yield return new TestCaseData("/test", "/test/Different", "Test Article Name", new DateTime(2012, 10, 21), new MvcHtmlString("
  • 21 Oct Test Article Name
  • ")); yield return new TestCaseData("/testing", "/test", "Test Article", new DateTime(2012, 09, 18), new MvcHtmlString("
  • 18 Sep Test Article
  • ")); yield return new TestCaseData("/News/Article1", "/News/Article1", "Test Article", new DateTime(2012, 06, 05), new MvcHtmlString("
  • 5 Jun Test Article
  • ")); yield return new TestCaseData("/", "/Hello", "Test Article", new DateTime(2013, 08, 25), new MvcHtmlString("
  • 25 Aug Test Article
  • ")); yield return new TestCaseData("/Snipe", "/Snipe", "Sniper in London", new DateTime(2011, 05, 15), new MvcHtmlString("
  • 15 May Sniper in London
  • ")); } }