Skip to content

Instantly share code, notes, and snippets.

@dddnuts
Last active May 17, 2016 16:54
Show Gist options
  • Save dddnuts/79e4543227e4bfaf136242ba1a226da3 to your computer and use it in GitHub Desktop.
Save dddnuts/79e4543227e4bfaf136242ba1a226da3 to your computer and use it in GitHub Desktop.
Create stub for a class invokes Action delegate.
using System;
public interface IUsersAPI
{
// GET api/users/:id
void GetUser(int id, Action<string> onSuccess, Action<string> onFailure);
}
using UnityEngine;
public class User
{
public int ID { get; private set; }
public string Name { get; private set; }
public static User ParseJSON(string userJSON)
{
var user = JsonUtility.FromJson<UserJSON>(userJSON);
return new User {
ID = user.id,
Name = user.name
};
}
private class UserJSON
{
public int id;
public string name;
}
}
using System;
public class UserProvider
{
private IUsersAPI api;
public UserProvider(IUsersAPI api)
{
this.api = api;
}
public void Get(int id, Action<User> onSuccess, Action<string> onFailure)
{
api.GetUser(id, (string json) =>
{
var user = User.ParseJSON(json);
onSuccess(user);
}, onFailure);
}
}
public class UserProviderTest
{
UserProvider sut;
[SetUp]
public void CreateStub()
{
var api = Substitute.For<IUsersAPI>();
api.GetUser(Arg.Is(1),
Arg.Invoke<string>("{ \"id\":1, \"name\":\"Aimee\" }"),
Arg.Any<Action<string>>());
api.GetUser(Arg.Is(2),
Arg.Any<Action<string>>(),
Arg.Invoke<string>("User not found"));
sut = new UserProvider(api);
}
// ...
}
public class UserProviderTest
{
// ...
[Test]
public void Get_ReturnsUser()
{
User result = null;
// The stub invokes the callbacks synchronously.
sut.Get(1, (User user) =>
{
result = user;
}, (string error) =>
{
Assert.Fail("Failure callback should not be called");
});
Assert.That(result, Is.Not.Null);
Assert.That(result.ID, Is.EqualTo(1));
}
// ...
}
public class UserProviderTest
{
// ...
[Test]
public void Get_WhenUserDoesNotExist_ReturnsError()
{
string result = null;
// The stub invokes the callbacks synchronously.
sut.Get(2, (User user) =>
{
Assert.Fail("Success callback should not be called");
}, (string error) =>
{
result = error;
});
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.EqualTo("User not found"));
}
}
using UnityEngine;
using UnityEditor;
using NUnit.Framework;
using NSubstitute;
using System;
public class UserProviderTest
{
UserProvider sut;
[SetUp]
public void CreateStub()
{
var api = Substitute.For<IUsersAPI>();
api.GetUser(Arg.Is(1),
Arg.Invoke<string>("{ \"id\":1, \"name\":\"Aimee\" }"),
Arg.Any<Action<string>>());
api.GetUser(Arg.Is(2),
Arg.Any<Action<string>>(),
Arg.Invoke<string>("User not found"));
sut = new UserProvider(api);
}
[Test]
public void Get_ReturnsUser()
{
User result = null;
// The stub invokes the callbacks synchronously.
sut.Get(1, (User user) =>
{
result = user;
}, (string error) =>
{
Assert.Fail("Failure callback should not be called");
});
Assert.That(result, Is.Not.Null);
Assert.That(result.ID, Is.EqualTo(1));
}
[Test]
public void Get_WhenUserDoesNotExist_ReturnsError()
{
string result = null;
// The stub invokes the callbacks synchronously.
sut.Get(2, (User user) =>
{
Assert.Fail("Success callback should not be called");
}, (string error) =>
{
result = error;
});
Assert.That(result, Is.Not.Null);
Assert.That(result, Is.EqualTo("User not found"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment