0

Riddle me this:

Given I have a class 'calculator.js' written in JavaScript

I would like to unit-test each method in this class - prefereably using C# - prefereably using dynamic invocation of the methods in the class

so, ideally:

[TEstMethod]
public void Divide_WhenDividendIsZero_ResultIsZero()
{
    // Arrange
    dynamic myJsClass = someFactory.CreateInstanceOf(myJsFilePath);

    // Act
    var result = (int) myJsClass.Divide(9, 0);

    // Assert
    result.ShouldEqual(0, "Division by zero should result in zero");
}

According to the hype, .Net 4.0 introduces the possibility to do this, however, I have not found any good tutorials on what I need to rig this up.

For the majority of my tests, WebDriver will do, however, for the "pure" javascript classes, this is my preferred way to do it - if it is possible at all?

1 Answer 1

2

You could load up IronJS (https://github.com/fholm/IronJS) and test the javascript using that.

It's not really .NET 4.0, but the DLR (Dynamic language runtime) that "makes this possible". IronJS will run on .NET 3.5 and there's experimental support for 2.0 and 3.0.

For specific documentation on how to run js with the library, this page will help: https://github.com/fholm/IronJS/wiki/Creating-a-hosting-context

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.