|
| 1 | +using System; |
| 2 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 3 | +using Moq; |
| 4 | +using RedditSharp.Things; |
| 5 | +using RedditSharp; |
| 6 | +using System.Net; |
| 7 | +using UnitTesting.TestData; |
| 8 | +using System.IO; |
| 9 | +using Newtonsoft.Json.Linq; |
| 10 | +using System.Linq; |
| 11 | +namespace UnitTesting |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Summary description for MoreUnitTest |
| 15 | + /// </summary> |
| 16 | + [TestClass] |
| 17 | + public class MoreUnitTest |
| 18 | + { |
| 19 | + |
| 20 | + private TestContext testContextInstance; |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + ///Gets or sets the test context which provides |
| 24 | + ///information about and functionality for the current test run. |
| 25 | + ///</summary> |
| 26 | + public TestContext TestContext |
| 27 | + { |
| 28 | + get |
| 29 | + { |
| 30 | + return testContextInstance; |
| 31 | + } |
| 32 | + set |
| 33 | + { |
| 34 | + testContextInstance = value; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + #region Additional test attributes |
| 39 | + // |
| 40 | + // You can use the following additional attributes as you write your tests: |
| 41 | + // |
| 42 | + // Use ClassInitialize to run code before running the first test in the class |
| 43 | + // [ClassInitialize()] |
| 44 | + // public static void MyClassInitialize(TestContext testContext) { } |
| 45 | + // |
| 46 | + // Use ClassCleanup to run code after all tests in a class have run |
| 47 | + // [ClassCleanup()] |
| 48 | + // public static void MyClassCleanup() { } |
| 49 | + // |
| 50 | + // Use TestInitialize to run code before running each test |
| 51 | + // [TestInitialize()] |
| 52 | + // public void MyTestInitialize() { } |
| 53 | + // |
| 54 | + // Use TestCleanup to run code after each test has run |
| 55 | + // [TestCleanup()] |
| 56 | + // public void MyTestCleanup() { } |
| 57 | + // |
| 58 | + #endregion |
| 59 | + |
| 60 | + [TestMethod] |
| 61 | + public void EnumerateComments() |
| 62 | + { |
| 63 | + Mock<WebAgent> mockWebAgent = new Mock<WebAgent>(MockBehavior.Strict); |
| 64 | + Mock<HttpWebRequest> mockRequest = new Mock<HttpWebRequest>(MockBehavior.Strict); |
| 65 | + Mock<HttpWebRequest> mockRequestMore1 = new Mock<HttpWebRequest>(MockBehavior.Strict); |
| 66 | + |
| 67 | + Mock<WebResponse> mockResponse = new Mock<WebResponse>(MockBehavior.Strict); |
| 68 | + Mock<WebResponse> mockResponseMore1 = new Mock<WebResponse>(MockBehavior.Strict); |
| 69 | + |
| 70 | + mockRequest.Setup(mr => |
| 71 | + mr.GetResponse() |
| 72 | + ).Returns(mockResponse.Object); |
| 73 | + |
| 74 | + mockRequestMore1.Setup(mr => |
| 75 | + mr.GetResponse() |
| 76 | + ).Returns(mockResponseMore1.Object); |
| 77 | + |
| 78 | + |
| 79 | + MemoryStream ms1 = new MemoryStream(); |
| 80 | + MemoryStream ms2 = new MemoryStream(); |
| 81 | + |
| 82 | + mockResponse.Setup(mr => mr.GetResponseStream()).Returns(ms1); |
| 83 | + mockResponseMore1.Setup(mr => mr.GetResponseStream()).Returns(ms2); |
| 84 | + |
| 85 | + mockWebAgent.Setup(wa => wa.CreateGet("/comments/post.json")).Returns(mockRequest.Object ); |
| 86 | + mockWebAgent.Setup(wa => wa.CreateGet("/api/morechildren.json?link_id=post&children=3,3-1,3-1-more,4,5,5-1,5-1-1,5-1-more&api_type=json")).Returns(mockRequestMore1.Object); |
| 87 | + |
| 88 | + |
| 89 | + mockWebAgent.Setup(wa => wa.GetResponseString(It.Is<Stream>(x=>x==ms1))).Returns(JsonGetComments.JsonComments()); |
| 90 | + mockWebAgent.Setup(wa => wa.GetResponseString(It.Is<Stream>(x => x == ms2))).Returns(More_3_4_5.GetMore_3_4_5()); |
| 91 | + |
| 92 | + Mock<Reddit> mockReddit = new Mock<Reddit>(MockBehavior.Strict); |
| 93 | + JToken jObject = JsonGetComments.GetComments()[0]["data"]["children"].First; |
| 94 | + |
| 95 | + Reddit r = mockReddit.Object; |
| 96 | + WebAgent waz = mockWebAgent.Object; |
| 97 | + |
| 98 | + |
| 99 | + Post p = new Post().Init(mockReddit.Object, jObject, mockWebAgent.Object); |
| 100 | + |
| 101 | + string[] fullNames = {"t1_1","t1_2","t1_3","t1_4","t1_5" }; |
| 102 | + Comment[] comments = p.EnumerateComments().ToArray(); |
| 103 | + Assert.AreEqual(fullNames.Count(), comments.Count()); |
| 104 | + |
| 105 | + foreach (var element in |
| 106 | + comments.Zip(fullNames, |
| 107 | + (c,fn)=>new { comment = c, fullName = fn })) |
| 108 | + { |
| 109 | + Assert.AreEqual(element.comment.FullName, element.fullName); |
| 110 | + } |
| 111 | + |
| 112 | + Assert.AreEqual(comments[0].Comments.Count,1); |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments