コード例 #1
0
        public void BuildsUrlBasedOnOutputAndEnvironment(
            string scriptOutput,
            string environment,
            string expectedUrl
            )
        {
            var urlHelper = new Mock <IUrlHelper>();

            urlHelper.Setup(x => x.Action(It.IsAny <UrlActionContext>())).Returns((UrlActionContext context) =>
            {
                var values = HtmlHelper.ObjectToDictionary(context.Values);
                // Super simple URL building, purely for test purposes
                var url = "/_routejs";
                if (values.ContainsKey("hash"))
                {
                    url += "/" + values["hash"];
                    if (values.ContainsKey("environment"))
                    {
                        url += "/" + values["environment"];
                    }
                }
                return(url);
            });
            var urlHelperFactory = new Mock <IUrlHelperFactory>();

            urlHelperFactory.Setup(x => x.GetUrlHelper(It.IsAny <ActionContext>())).Returns(urlHelper.Object);

            var routeJs = new Mock <IRouteJs>();

            routeJs.Setup(x => x.GetJavaScript(It.IsAny <bool>())).Returns(scriptOutput);
            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider.Setup(x => x.GetService(typeof(IRouteJs))).Returns(routeJs.Object);
            var hostingEnvironment = new Mock <IHostingEnvironment>();

            hostingEnvironment.Setup(x => x.EnvironmentName).Returns(environment);
            var actionContextAccessor = new Mock <IActionContextAccessor>();

            var helper = new RouteJsHelper(
                urlHelperFactory.Object,
                serviceProvider.Object,
                hostingEnvironment.Object,
                actionContextAccessor.Object
                );

            RouteJsHelper.ClearCache();
            var result = helper.Render();

            Assert.Equal($"<script src=\"{expectedUrl}\"></script>", result.ToString());
        }
コード例 #2
0
		public void BuildsUrlBasedOnOutputAndEnvironment(
			string scriptOutput, 
			string environment,
			string expectedUrl
		)
		{
			var urlHelper = new Mock<IUrlHelper>();
			urlHelper.Setup(x => x.Action(It.IsAny<UrlActionContext>())).Returns((UrlActionContext context) =>
			{
				var values = HtmlHelper.ObjectToDictionary(context.Values);
				// Super simple URL building, purely for test purposes
				var url = "/_routejs";
				if (values.ContainsKey("hash"))
				{
					url += "/" + values["hash"];
					if (values.ContainsKey("environment"))
					{
						url += "/" + values["environment"];
					}
				}
				return url;
			});

			var routeJs = new Mock<IRouteJs>();
			routeJs.Setup(x => x.GetJavaScript(It.IsAny<bool>())).Returns(scriptOutput);
			var serviceProvider = new Mock<IServiceProvider>();
			serviceProvider.Setup(x => x.GetService(typeof (IRouteJs))).Returns(routeJs.Object);
			var hostingEnvironment = new Mock<IHostingEnvironment>();
			hostingEnvironment.Setup(x => x.EnvironmentName).Returns(environment);

		    var helper = new RouteJsHelper(
				urlHelper.Object, 
				serviceProvider.Object, 
				hostingEnvironment.Object
			);

			RouteJsHelper.ClearCache();
			var result = helper.Render();
			Assert.Equal($"<script src=\"{expectedUrl}\"></script>", result.ToString());
		}