Currently we are working on a new project which utilizes a class library. I'm developing a feature for the site, working on the basics of a repository which right now has simple CRUD operation methods talking to a database. We have recently decided to start trying to unit-test our code, to further encourage good programming habits.
Right now my Unit Test is failing because it is trying to use a stored procedure on our Live DB - one that does not currently exist on the Live. At first it simply failed (threw an exception) and I spoofed the HttpContext and handed it the server name URL which should trigger connecting to the test DB. However it is connecting to live currently The code which I believe to be causing this is:
public static bool TestMode()
{
if (
HttpContext.Current.Request.ServerVariables["SERVER_NAME"] == "localhost" ||
HttpContext.Current.Request.ServerVariables["SERVER_NAME"] == "test.org"
)
return true;
else
return false;
}
The library is intended to be used in combination with an MVC site, so this type of set up is not uncalled for, and unfortunately it is not my choice on refactoring how we do this.
This being said here's my unit test
[TestClass]
public class CalendarTests {
public CalendarRepository _repo { get; set; }
[TestInitialize]
public void Setup() {
_repo = new CalendarRepository();
HttpContext.Current = new HttpContext(
new HttpRequest(null, "http://test.org", null),
new HttpResponse(null)
);
}
[TestMethod]
public void CreateEventTest() {
int val = _repo.CreateEvent(1,
"test title",
"demo-description",
DateTime.Now,
DateTime.Now.AddDays(1),
true,
1,
1);
Assert.IsTrue(val > 0);
}
}
Most of the searches I've done are having this issue because they're testing a controller on an MVC project, this is however slightly different and I'm unsure of where to go from here. I know I need to find a way to mock the context in order to test. Various testing frameworks are suggested all over like NUnit, and also mocking frameworks like NSubstitute. I'm interested in these but unsure if their use is warranted.
Any and all help is appreciated!
EDIT:
What I've tried:
Although I deleted the code I attempted to write something similar to this as well as this
EDIT2:
This is where the connection string comes from
public static string DSHA()
{
var db = new Database()
{
LiveConnectionString = "sanitized-live",
TestConnectionString = "sanitized-test"
};
return ResolveConnection(db);
}
This is the code TestMode is called by
private static string ResolveConnection(Database db)
{
if (Helpers.TestMode())
return db.TestConnectionString;
else
return db.LiveConnectionString;
}
So I ask for the connection string, which calls DSHA, resulting in the evaluation from ResolveConnection whose conditional is TestMode ultimately deciding the resulting returned connection string.
EDIT2:
Nkosi's reply has been extremely hlepful, however now I'm trying to ensure that Helpers is getting the default ServerVariables genuinely supplied by the server. I tried to use a constructor
public Helpers(){
ServerVariable = new ServerVariables();
}
However when I check the Context in a unit test I get a null reference exception, I'm thinking it's because Helpers has static methods iwhtin it, but the Helpers class itself is not static.
[TestMethod]
public void CheckContext(){
var context = Helpers.ServerVariable.Name;
Assert.IsTrue(context == "localhost");
}
I'm unsure of proper ways to inject the interface using a container but want to avoid changing the Helpers class behavior as much as possible as there's quite a bit of code written using it. Needing to supply it anything else could mean quite a bit of hunting to ensure it's behavior is changed everywhere.