Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
IdeaStrike Test Project Nancy Autofac Bootstrapper
//
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Linq.Expressions;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
//
using Ideastrike;
using Ideastrike.Nancy.Infrastructure.Logging;
using Ideastrike.Nancy.Infrastructure.Repositories;
using Ideastrike.Nancy.Models;
using Ideastrike.Nancy.Modules;
//
using Moq;
//
using Nancy;
using Nancy.Testing;
namespace IdeaStrike.Tests
{
public class IdeaStrikeSpecBase<TModule> where TModule : BaseModule
{
protected ConfigurableBootstrapper Bootstrapper;
protected Browser Browser;
protected BrowserResponse Response;
protected Mock<ILogger> _Logger = new Mock<ILogger>();
protected Mock<IUserRepository> _Users = new Mock<IUserRepository>();
protected Mock<IIdeaRepository> _Ideas = new Mock<IIdeaRepository>();
protected Mock<IFeatureRepository> _Features = new Mock<IFeatureRepository>();
protected Mock<IActivityRepository> _Activity = new Mock<IActivityRepository>();
protected Mock<ISettingsRepository> _Settings = new Mock<ISettingsRepository>();
//protected Mock<IdeastrikeContext> _Context = new Mock<IdeastrikeContext>();
protected Mock<IImageRepository> _Images = new Mock<IImageRepository>();
public IdeaStrikeSpecBase()
{
Bootstrapper = new ConfigurableBootstrapper(with =>
{
with.Module<TModule>();
with.Dependencies(_Logger.Object, _Users.Object, _Ideas.Object, _Features.Object, _Activity.Object, _Settings.Object, _Images.Object);
with.DisableAutoRegistration();
with.NancyEngine<NancyEngine>();
});
}
protected User CreateMockUser(string username)
{
var user = new User {
Id = Guid.NewGuid(),
UserName = username,
UserClaims = new Collection<UserClaim> {new UserClaim {Claim = new Claim {Name = "admin"}} }
};
_Users.Setup(d => d.Get(user.Id)).Returns(user);
_Users.Setup(d => d.GetUserFromIdentifier(user.Id)).Returns(user);
_Users.Setup(d => d.FindBy(It.IsAny<Expression<Func<User, bool>>>())).Returns(new[] { user }.AsQueryable());
return user;
}
protected void EnableFormsAuth()
{
FormsAuthentication.Enable(Bootstrapper, new FormsAuthenticationConfiguration
{
RedirectUrl = "~/login",
UserMapper = _Users.Object
});
}
protected byte[] CreateImageBits()
{
Bitmap img = new Bitmap(10, 10);
Graphics imgData = Graphics.FromImage(img);
imgData.DrawLine(new Pen(Color.Blue), 0, 0, 10, 10);
byte[] imageBits = null;
using (MemoryStream memoryStream = new MemoryStream())
{
img.Save(memoryStream, ImageFormat.Bmp);
imageBits = memoryStream.ToArray();
}
return imageBits;
}
protected void Get(string path, Action<BrowserContext> browserContext = null)
{
Browser = new Browser(Bootstrapper);
Response = Browser.Get(path, browserContext);
}
protected void Post(string path, Action<BrowserContext> browserContext = null)
{
Browser = new Browser(Bootstrapper);
Response = Browser.Post(path, browserContext);
}
protected void Put(string path, Action<BrowserContext> browserContext = null)
{
Browser = new Browser(Bootstrapper);
Response = Browser.Put(path, browserContext);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment