Skip to content

Instantly share code, notes, and snippets.

@k0stya
Created December 4, 2013 18:34
Show Gist options
  • Save k0stya/7792969 to your computer and use it in GitHub Desktop.
Save k0stya/7792969 to your computer and use it in GitHub Desktop.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
namespace UrlShortener.Tests
{
[TestClass]
public class UrlShortener_Tests
{
private Shortener _service;
private Mock<IShortenerEngine> _engine;
private Mock<IRepository> _repMock;
[TestInitialize]
public void TestIntialize()
{
_engine = new Mock<IShortenerEngine>();
_repMock = new Mock<IRepository>();
_service = new Shortener(_engine.Object, _repMock.Object);
}
[TestMethod]
public void GetShortUrl_should_pass_url_to_shortenerEngine()
{
//Arrange
string longUrl = "http://super.long.url.com/blablabla";
// Act
_service.GetShortUrl(longUrl);
// Assert
_engine.Verify(e => e.GetShortUrl(longUrl), Times.Once);
}
[TestMethod]
public void GetShortUrl_should_save_short_url_to_repository()
{
// Arrange
string originalUrl = "http://super.long.url.com/blablabla";
string shortUrl = "http://s.com/1";
_engine.Setup(s => s.GetShortUrl(originalUrl))
.Returns(shortUrl);
// Act
_service.GetShortUrl(originalUrl);
// Assert
_repMock.Verify(r => r.Save(shortUrl, originalUrl));
}
}
public interface IRepository
{
void Save(string shortUrl, string originalUrl);
}
public class Shortener : IShortener
{
private readonly IShortenerEngine _shortenerEngine;
private readonly IRepository _repository;
public Shortener(IShortenerEngine shortenerEngine, IRepository repository)
{
_shortenerEngine = shortenerEngine;
_repository = repository;
}
public string GetShortUrl(string url)
{
string shortUrl = _shortenerEngine.GetShortUrl(url);
_repository.Save(shortUrl, url);
return shortUrl;
}
}
public interface IShortener
{
string GetShortUrl(string url);
}
public interface IShortenerEngine
{
string GetShortUrl(string url);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment