-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnitTest1.cs
83 lines (60 loc) · 2.4 KB
/
UnitTest1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json.Linq;
using RestSharp;
using System;
using System.Diagnostics;
using System.Collections.Generic;
namespace RestSharpSpeckflow
{
[TestClass]
public class UnitTest1
{
[ClassInitialize]
public static void BeforeAnyTests(TestContext context)
{
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
process.StandardInput.WriteLine("cd jsonServer");
process.StandardInput.WriteLine("json-server .\\db.json --port 3004");
}
[TestMethod]
public void GetMethod()
{
//Store client url
var client = new RestClient("http://localhost:3004/");
//Create request
var request = new RestRequest("posts/{postid}", Method.Get);
request.AddUrlSegment("postid",6);
//Execute and store request response
var response = client.GetAsync(request);
//Deseriailze response json objects
JObject obs = JObject.Parse(response.Result.Content);
Assert.AreEqual("Ruby", obs["author"].ToString(), "Author is not correct");
System.Console.WriteLine(obs.ToString());
}
[TestMethod]
public void PostMethod()
{
//Store client url
var client = new RestClient("http://localhost:3004/");
//Create request
var request = new RestRequest("posts/", Method.Post);
request.AddBody(new Model.Posts() { id = "1",title = "Pistols",author = "John Wayne"});
var response = client.PostAsync(request);
JObject obs = JObject.Parse(response.Result.Content);
Assert.AreEqual("John Wayne", obs["author"].ToString(), "Post was added correctly.");
System.Console.WriteLine(response.ToString());
}
[ClassCleanup]
public static void AfterAllTests()
{
Process process = new Process();
process.Close();
}
}
}