Testing .NET Web API from JavaScript - javascript

I have a Visual Studio solution with two projects in it.
First project is a console application written in F#. The console application is actually a simple server with web API implementation.
Second project is a web application written in JavaScript. This project contains corresponding implementation of API, which allows users to make API requests to the server.
I need to test this client-side implementation of API to be sure that it passes the right data to the server and receives the right data back. What is the best practice to achieve this?
The problem is that I need to build and run the first project before I can run tests in a second project. What is also would be good is to make these tests running on Continuous Integration. If it is matter, I use visualstudio.com for CI.
Is this possible at all without manual start of first project and then running tests?
Note that on each test run it is important to restart console application.

Yes it is possible. I would recommend you to have a look at RAML which is a way of
Documenting your API
Using the documentation to generate a client and use it for automation tests
Using Anypoint you can expose your documentation and get your stakeholders to easily test it as well.
Please have a look at Design, test and document RESTful APIs using RAML in .NET for a full run through and explanation. In this blog post I used .NET at my language of choice, but the same principle applies to other languages.
With this you can test your API before you write the client. The Anypoint platform also allows you to mock the webservice and you can use the mock for testing you client later on.
This way you can test both applications in isolation and in a programmatic/automated manner (which you can execute from your CI). RAML has also a library for generating a JavaScript client from a RAML file: https://github.com/mulesoft/raml-client-generator

Related

How JavaScript Program Can Communicate With C# Console Application?

I'm trying to write a program that is composed of two parts. The logic part that I prefer to write in C# language because it's object oriented and more important it's the language witch I know the best among objective languages, and the graphic part witch I want to write it with Html, Css and JavaScript and again because they're the most familiar graphical languages witch I can use. Now my question is that how can I transfer data between a C# console app and a JavaScript browser app.
Any help will be appreciated.
Edit:
I must clarify that I just need C# console app and not a web app or web api, and therefor this part is not a backend project. And I need to run these two program separately and want them to communicate with each other.
What you describe should be a full stack application to be honest
With ASP.NET you create your C# backend, setup controllers which handle the interaction between backend code and the front end, and then on the front end you can utilize both Razor markup for HTML templating alongside standard Javascript.
With Blazor web apps, you can take it a step further and have your backend code compile to web assembly and execute directly on the client engine, but it also integrates javascript interop as well. Just be mindful about whether you want a client side or server side Blazor app, as each do things a certain way and are meant for different use cases
What you describe though, sounds more precisely like you want a seperate javascript app, which should be in communication with an ASP.NET Web API. This way you create your backend and api endpoints, which the javascript app can then make requests against. You can start your research for that with MS's tutorial here
The dotNet command line tools can generate solutions for all of these, an ASP.NET fullstack web app, ASP.NET Web API solution, as well as the various blazor app options. And of course, the standard IDE's like Visual Studio and Rider can create these solutions as well
If you're looking to have direct interop, blazor might be what you're looking for.
https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/call-javascript-from-dotnet?view=aspnetcore-6.0

Setting up TestCafe with Rails backend and fixtures data

I'm currently working on a Rails project that uses RSpec as the testing framework. We have different Rails fixture data that helps us test our UI when we're in different states.
However, I've been intrigued with using TestCafe to handle our functional UI testing. I've played around with various examples of the website and it seems great to use. But the one part holding me back from introducing it into my project is I'm not sure how to use TestCafe to set up something equivalent to Rails fixture data - i.e. I want to use TestCafe to set up different scenarios where my database is in a particular state so that I can then test the UI.
I've tried searching through TestCafe's documentation but haven't had much luck since TestCafe seems to use the word "fixtures" to represent a particular page you're testing and not the mock data.
Unfortunately there is no built-in support for handling test databases. But because tests are executed in Node.js environment, you can use e.g. db-migrate and js-yaml modules to load your fixtures to the test database.

Is it possible to show a WPF Application on a html file in my Nodejs application

A few friends of mine and me started to work on a school project, which mainly focuses on Speech Analysis. They wanted to make an application in WPF and I tried to do the Analysis in different programming languages. Mainly C#, Python and JavaScript. I found the perfect way to analyze the Speech-Input in a way we wanted it, but the Programm is written in JavaScript, running in a Nodejs Server.
I searched for a way to implement the Nodejs Server in C# but had some difficulties, so I looked around and found EdgeJs which works the other way around.
Basically making it possible work with .Net and C# Code in JavaScript.
All I really need to do is call functions from my Nodejs application within the C# Code.
The problem is, I have no idea how to get a whole WPF application into the NodejS application, i only found examples with console applications.
Would really aprreciate your input and maybe you know a way to solve my problem :)
Have you thought of making a Webservice for your C# application and then calling your C# functions using Node and AJAX via the webservice
Depending on the input/output you're looking for, you could add some Restful Web Api endpoints to your Node application/server. These rest endpoints will call the speech-analysis functions internally, and then return the results.
If you run your WPF app on the same machine as your node server, you should be able to easily make calls to the API and get the results you are looking for.

How to do a smoke Test and Acceptance test in a Javascript Aplication?

I want to do a smoke test in order to test the connection between my web app and the server itself. Does Someone know how to do it? In addition I want to do an acceptance tests to test my whole application. Which tool do you recommend?
My technology stack is: backbone and require.js and jquery mobile and jasmine for BDD test.
Regards
When doing BDD you should always mock the collaborators. The tests should run quickly and not depend on any external resources such as servers, APIs, databases etc.
The way you would want to make in f.e. Jasmine is to declare a spy that pretends to be the server. You then move on to defining what would be the response of the spy in a particular scenario or example.
This is the best aproach if you want your application to be environment undependent. Which is very needed when running Jenkins jobs - building a whole infrastructure around the job would be hard to reproduce.
Make spy/mock objects that represent the server and in your specs define how the external sources behave - this way you can focus on what behavior your application delivers under specified circumstances.
This isn't a complete answer, but one tool we've been using for our very similar stack is mockJSON. It's a jQuery plugin that does a nice job both:
intercepting calls to a URL and instead sending back mock data and
making it easy to generate random mock data based on templates.
The best part is that it's entirely client side, so you don't need to set up anything external to get decent tests. It won't test the actual network connection to your server, but it can do a very good job validating that type of data your server would be kicking back. FWIW, we use Mocha as our test framework and haven't had any trouble getting this to integrate with our BDD work.
The original mockJSON repo is still pretty good, though it hasn't been updated in a little while. My colleagues and I have been trying to keep it going with patches and features in my own fork.
I found a blog post where the author explain how to use capybara, cucumber and selenium outside a rails application and therefore can be use to test a javascript app. Here are the link: http://testerstories.com/?p=48

Does RhinoJS support the websockets API?

I have been using Jasmine to write BDD tests for a web app. I am interested in running these same tests from the command line using Rhino, and I found this blog entry which has been very helpful. However; my app uses pusher, which offers a simple API for passing events between clients using web sockets.
My tests run fine from a browser, but running them via Rhino fails to connect to pusher. Pusher offers a debug stream, and it would show authentication errors or other failures, but it doesn't register any activity when I run my tests from the command line.
This may be a simple yes or no answer, but I haven't been able to locate any relevant documentation, and I'm brand new to Rhino so I apologize if this is a total n00b question.
My question
As the title says, does anyone know if Rhino supports the websockets API? Am I doing something else wrong here, or will scripts that rely on websockets simply not work via Rhino?
My overall goal was to use my already written jasmine tests as stress tests by running them on several EC2 instances at once; is there a good (simple?) way to do this while reusing my existing javascript tests, or should I suck it up and just write my stress tests in a server side scripting language? I am aware of selenium grid, but was hoping to avoid having to spawn new browsers to run these tests, if possible.
Thanks so much!
Ringo, a Rhino-based CommonJS runtime supports them: http://ringojs.org/api/v0.6/ringo/webapp/websocket/

Categories