示例#1
0
        public async void Basic_Web_Call_Succeeds()
        {
            string script = @"
                var client = new httpClient();
                var getResult = client.GetAsync('http://www.google.com/').Result;
                context.Subject.TestValueObject = getResult;
                context.Subject.TestValueString = getResult.Content.ReadAsStringAsync().Result;
                ";

            var testNode = new JavaScriptNode <TestObjectA>
            {
                ExecutedScript = script
            };

            testNode.AddScriptType("httpClient", typeof(HttpClient));

            var testObject = new TestObjectA();

            var context = new ExecutionContext <TestObjectA>(testObject);

            var result = await testNode.ExecuteAsync(context);

            testObject = result.GetSubjectAs <TestObjectA>();

            testObject.TestValueObject.ShouldNotBeNull();

            testObject.TestValueString.ShouldNotBeNull();
            testObject.TestValueString.ShouldNotBeEmpty();

            result.Status.ShouldEqual(NodeResultStatus.Succeeded);
            testNode.Status.ShouldEqual(NodeRunStatus.Completed);
        }
        public async void Node_Is_Not_Run_If_ShouldExecute_False()
        {
            var testNode = new JavaScriptNode <TestObjectA>
            {
                ExecutedScript    = "context.Subject.TestValueString = 'Hello JavaScript!';",
                ShouldExecuteFunc = c => Task.FromResult(false)
            };

            var testObject = new TestObjectA();

            var context = new ExecutionContext <TestObjectA>(testObject);

            var result = await testNode.ExecuteAsync(context);

            result.Status.ShouldEqual(NodeResultStatus.NotRun);

            context.Subject.TestValueString.ShouldBeNull();
        }
        public async void Failed_Node_Run_Status_Is_Failed()
        {
            var testNode = new JavaScriptNode <TestObjectA>
            {
                ExecutedScript = "context.Subject.TestValueString = 'Hello JavaScript!'; result.IsSuccess = false;"
            };

            var testObject = new TestObjectA();

            var context = new ExecutionContext <TestObjectA>(testObject);

            var result = await testNode.ExecuteAsync(context);

            result.GetSubjectAs <TestObjectA>().TestValueString.ShouldEqual("Hello JavaScript!");

            testNode.Status.ShouldEqual(NodeRunStatus.Completed);
            result.Status.ShouldEqual(NodeResultStatus.Failed);
        }
        public async void Successful_Node_Run_Matches_Expectations()
        {
            var testNode = new JavaScriptNode <TestObjectA>
            {
                ExecutedScript = "context.Subject.TestValueString = 'Hello JavaScript!'"
            };

            var testObject = new TestObjectA();

            var context = new ExecutionContext <TestObjectA>(testObject);

            var result = await testNode.ExecuteAsync(context);

            result.GetSubjectAs <TestObjectA>().TestValueString.ShouldEqual("Hello JavaScript!");

            result.Status.ShouldEqual(NodeResultStatus.Succeeded);
            testNode.Status.ShouldEqual(NodeRunStatus.Completed);
        }
        public async void Errant_Node_Run_Status_Is_Faulted()
        {
            var testNode = new JavaScriptNode <TestObjectA>
            {
                ExecutedScript = "context.Subject.TestValueString = 'Hello JavaScript!'; throw 'An error happened';"
            };

            var testObject = new TestObjectA();

            var context = new ExecutionContext <TestObjectA>(testObject);

            var result = await testNode.ExecuteAsync(context);

            result.Exception.ShouldNotBeNull();

            result.Status.ShouldEqual(NodeResultStatus.Failed);

            testNode.Status.ShouldEqual(NodeRunStatus.Faulted);
        }
        public void Errant_Node_Run_With_ThrowOnError_True_Throws()
        {
            var testNode = new JavaScriptNode <TestObjectA>
            {
                ExecutedScript = "context.Subject.TestValueString = 'Hello JavaScript!'; throw 'An error happened';"
            };

            var testObject = new TestObjectA();

            var context = new ExecutionContext <TestObjectA>(testObject)
            {
                GlobalOptions = { ThrowOnError = true }
            };

            Assert.Throws <Microsoft.ClearScript.ScriptEngineException>(async() => await testNode.ExecuteAsync(context));
        }