I use Moq to test some behaviour. I want to verify set view's model property as new instance of Message class:
[TestFixture]
public class MessageFormPresenterTests
{
private NameValueCollection queryString;
private Mock<IDatabase> mockDatabase;
private Mock<HttpContextBase> mockHttpContext;
private Mock<HttpRequestBase> mockRequest;
private Mock<HttpResponseBase> mockResponce;
private Mock<IMessageFormView> mockView;
private MessageFormPresenter presenter;
[SetUp]
public void SetUp()
{
this.queryString = new NameValueCollection();
this.mockDatabase = new Mock<IDatabase>();
this.mockHttpContext = new Mock<HttpContextBase>();
this.mockRequest = new Mock<HttpRequestBase>();
this.mockResponce = new Mock<HttpResponseBase>();
this.mockView = new Mock<IMessageFormView>();
this.mockHttpContext.SetupGet(c => c.Request).Returns(this.mockRequest.Object);
this.mockHttpContext.SetupGet(c => c.Response).Returns(this.mockResponce.Object);
this.mockRequest.SetupGet(r => r.QueryString).Returns(this.queryString);
this.presenter = new MessageFormPresenter(this.mockView.Object) { Database = this.mockDatabase.Object, HttpContext = this.mockHttpContext.Object };
}
[Test]
public void ViewLoad_QueryStringNotHasMessageIdParameter_PopulateViewModelAsNewMessage()
{
// Act
this.mockView.Raise(v => v.Load += null, new EventArgs());
// Assert
this.mockView.VerifySet(v => v.Model = It.Is<Message>(m => m.Id == 0));
}
}
But this test pass even if i not write this functionality in my MessageFormPresenter. When i set breakpoint to action call (v.Model = It.Is(m => m.Id == 0)) and debug my test, Model property returned as "Castle.Proxies.MessageProxy" type and has all properties with default values (value of Id property is 0, of course).
Why is this happening? I don't setup anything to return and Moq should return null by default.
P.S. Excuse me for my poor english. I hope you understand what i'm talking about)
UPDATE1: MessageFormPresenter class:
public class MessageFormPresenter : Presenter<IMessageFormView>
{
private IDatabase database;
public MessageFormPresenter(IMessageFormView view)
: base(view)
{
View.Load += this.View_Load;
}
public IDatabase Database
{
get { return this.database ?? (this.database = DatabaseFactory.DatabaseInstance); }
set { this.database = value; }
}
private void View_Load(object sender, EventArgs e)
{
int messageId;
var messageParam = Request.QueryString[QueryParamNames.MessageId];
if (messageParam == null)
{
View.Model = new Message();
return;
}
if (!View.IsAdmin)
{
Response.Redirect(PageUrls.AccessDenied, true);
return;
}
if (int.TryParse(
messageParam,
NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite,
CultureInfo.InvariantCulture,
out messageId))
{
View.Model = this.Database.Single<Message>(messageId);
}
else
{
Response.Redirect(PageUrls.DefaultPage, true);
}
}
}
Base class Presenter<IMessageFormView> provided by WebFormsMvp framework.
UPDATE2: If i use strict behaviour TargetInvocationException will be thrown in base constructor of MessageFormPresenter, even if i setup all properties of Mock<IMessageFormView>.
Related
Is it possible to add values to the customDimensions property of records in requests in ApplicationInsights? How?
I've found that logger.BeginScope can add them to traces -- the value must be Dictionary<string, object> (Passing Dictionary<string, string> does not work.)
Adding the properties to Activity.Current.AddTag has no effect.
Nor does adding properties to TelemetryClient.Properties.
(This is deprecated but nobody seems to know what to do instead.)
The answer is to use a custom ITelemetryInitializer like this
internal class TelemetryInitializer : ITelemetryInitializer
{
private static readonly string[] _Headers = new string[] { "Referer" /*sic */, "X-Forwarded-For", };
private readonly IHttpContextAccessor _httpContextAccessor;
public TelemetryInitializer(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public void Initialize(ITelemetry item)
{
ISupportProperties tcp = item as ISupportProperties;
if (item == null || _httpContextAccessor.HttpContext == null) { return; }
LogTags tags = new LogTags();
tags.AddTag("BuildName", BuildNameAttribute.GetBuildName());
tags.AddTag("Method", _httpContextAccessor.HttpContext.Request.Method);
tags.AddTag("Host", _httpContextAccessor.HttpContext.Request.Host.Value);
// RequestPath is added automaticallt.
tags.AddTags(_httpContextAccessor.HttpContext.Request.Headers.Where(z => _Headers.Contains(z.Key)).SelectMany(z => z.Value.Select(y => new KeyValuePair<string, string>(z.Key, y))));
tags.AddTags(_httpContextAccessor.HttpContext.Request.Query.SelectMany(z => z.Value.Select(y => new KeyValuePair<string, string>(z.Key, y))));
if (_httpContextAccessor.HttpContext.Request.HasFormContentType && _httpContextAccessor.HttpContext.Request.Form != null)
{
tags.AddTags(_httpContextAccessor.HttpContext.Request.Form.Where(z => z.Key.ToLower().Contains("password")).SelectMany(z => z.Value.Select(y => new KeyValuePair<string, string>(z.Key, y.Length > 21 ? y.Substring(0, 21) + "..." : y))));
tags.AddTags(_httpContextAccessor.HttpContext.Request.Form.Files.Select(z => new KeyValuePair<string, string>("File", $"{z.ContentType} '{z.FileName}' {z.Length}")));
}
if (_httpContextAccessor.HttpContext.User.Identity?.IsAuthenticated ?? false)
{
tags.AddTags(_httpContextAccessor.HttpContext.User.Claims.Select(z => new KeyValuePair<string, string>(z.Type, z.Value)));
}
else
{
tags.AddTag("authentication", "none");
}
foreach (KeyValuePair<string, object> tag in tags.GetAllTags())
{
tcp.Properties[tag.Key] = tag.Value.ToString();
}
}
}
and
public static ILoggingBuilder AddLogging(this ILoggingBuilder builder, bool isDevelopment)
{
builder.ClearProviders();
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
//builder.Services.AddApplicationInsightsTelemetry("key");
//Microsoft.ApplicationInsights.Extensibility.TelemetryConfiguration.Active.TelemetryChannel.DeveloperMode = true;
if (isDevelopment)
{
builder.AddBetterConsoleFormatter();
//builder.AddHttpRequestLogger(); // Just can't get this to work :(
//builder.AddBetterConsoleLogger(); // The singleton doesn't work very well and I don't understand why correlation IDs are omitted/
//builder.AddConsole();
builder.AddDebug();
}
else
{
// Add ApplicationInsights
builder.AddApplicationInsights();
builder.Services.AddApplicationInsightsTelemetry();
builder.Services.AddSingleton<ITelemetryInitializer, TelemetryInitializer>();
}
The IHttpContextAccessor deals with all that nastyness -- I was surprised to find that this is a singleton rather than scoped.
It turns out that this is not called until late on in the request pipeline which means that the auth middleware has run and therefore the .User is populated.
I would like to get the value of Result which comes with invocation.ReturnValue , if it is not async , there is no problem. If the method is async, I cannot get the Result of it
public class RedisCacheAspect: MethodInterception
{
private int _duration;
private IRedisCacheManager _redisCacheManager;
private static readonly ConcurrentDictionary<Type, MethodInfo> TypeofTaskResultMethod = new ConcurrentDictionary<Type, MethodInfo>();
public RedisCacheAspect(int duration = 60)//Default 60
{
_duration = duration;
_redisCacheManager = ServiceTool.ServiceProvider.GetService<IRedisCacheManager>();
}
public override void Intercept(IInvocation invocation)
{
var methodName = string.Format($"{invocation.Method.ReflectedType.FullName}.{invocation.Method.Name}");
var method = invocation.Method;
var arguments = invocation.Arguments.ToList();
var key = KeyGenerator.GetCacheKey(invocation.Method, invocation.Arguments,"FoodThen");
var returnType = invocation.Method.ReturnType;
var isExists = _redisCacheManager.IsAdd(key);
var isAsync = IsAsyncMethod(method);
if (isExists)
{
string cacheValue = GetCacheAsync(key);
var objValue = DeserializeCache(key, cacheValue, returnType);
invocation.ReturnValue = ResultFactory(objValue, returnType, isAsync);
return;
}
invocation.Proceed();
_redisCacheManager.Set(key, invocation.ReturnValue,TimeSpan.FromMinutes(_duration));
}
DeserializeCache Method :
private object DeserializeCache(string cacheKey, string cacheValue, Type returnType)
{
try
{
return JsonConvert.DeserializeObject(cacheValue, returnType);
}
catch (System.Exception)
{
_redisCacheManager.Remove(cacheKey);
return null;
}
}
ResultFactory Method :
private object ResultFactory(object result, Type returnType, bool isAsync)
{
if (isAsync)
{
return TypeofTaskResultMethod
.GetOrAdd(returnType, t => typeof(Task)
.GetMethods()
.First(p => p.Name == "FromResult" && p.ContainsGenericParameters)
.MakeGenericMethod(returnType))
.Invoke(null, new object[] { result });
}
else
{
return result;
}
}
This is how invocation.ReturnValue looks like and I want to get the value of Result...
Since you've debugged,have you noticed the value of returntype after var returnType = invocation.Method.ReturnType; been executed?
For Async methods ,the return type should be:
invocation.Method.ReturnType.GenericTypeArguments.First();
Can someone explain to me how to create an instance of this component in a Moq TestMethod? Here is the definition of the class. I need to test the ProcessAutomaticFillRequest method.
public class AutomaticDispenserComponent : IAutomaticDispenserComponent
{
private readonly Lazy<IMessageQueueComponent> _messageQueueComponent;
protected IMessageQueueComponent MessageQueueComponent { get { return _messageQueueComponent.Value; } }
public AutomaticDispenserComponent(Func<IMessageQueueComponent> messageQueueComponentFactory)
{
_messageQueueComponent = new Lazy<IMessageQueueComponent>(messageQueueComponentFactory);
}
public void ProcessAutomaticFillRequest(FillRequestParamDataContract fillRequestParam)
{
if (fillRequestParam.PrescriptionServiceUniqueId == Guid.Empty)
throw new InvalidOperationException("No prescription service was specified for processing fill request.");
if (fillRequestParam.Dispenser == null)
throw new InvalidOperationException("No dispenser was specified for processing fill request.");
var userContext = GlobalContext.CurrentUserContext;
var channel = string.Format(Channel.FillRequest, userContext.TenantId,
userContext.PharmacyUid, fillRequestParam.Dispenser.DeviceAgentUniqueId);
NotificationServer.Publish(channel, fillRequestParam);
}
Here is how I started my test, but I don't know how to create an instance of the component:
[TestMethod]
[ExpectedException(typeof (InvalidOperationException))]
public void FillRequestFailsWhenPrescriptionServiceUniqueIdIsEmpty()
{
// How do I create an instance of automatiqueDispenserComponent here
// since there is Func as constructor parameter?
var fillRequestParam = new FillRequestParamDataContract
{
PrescriptionServiceUniqueId = Guid.Empty
};
_automaticDispensercomponent.ProcessAutomaticFillRequest(fillRequestParam);
// ...
}
Updated the answer based on the comments below. You need to mock the Func parameter for the test.
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void FillRequestFailsWhenPrescriptionServiceUniqueIdIsEmpty()
{
var mockMsgQueueComponent = new Mock<Func<IMessageQueueComponent>>();
var _automaticDispensercomponent = new AutomaticDispenserComponent
(mockMsgQueueComponent.Object);
var fillRequestParam = new FillRequestParamDataContract
{
PrescriptionServiceUniqueId = Guid.Empty
};
_automaticDispensercomponent.ProcessAutomaticFillRequest(fillRequestParam);
}
I am attempting to write some Tests for a small project of mine but they seem to fail (I am starting with 1 test 'Create_Class')
I use the repository pattern and use Constructor Dependency Injection:
public KlasController() {
db = ObjectContextPerHttpRequest.Context;
KlasRepo = new KlasRepository(db);
LesRepo = new LesRepository(db);
OpdrachtRepo = new OpdrachtRepository(db);
}
//dependency injection constructor
public KlasController(IKlasRepository KlasRepo, ILesRepository LesRepo,
IOpdrachtRepository OpdrachtRepo) {
this.KlasRepo = KlasRepo;
this.LesRepo = LesRepo;
this.OpdrachtRepo = OpdrachtRepo;
}
here is my TestClass with testinitializer (which runs before every test) and the first test
[TestClass()]
public class KlasControllerTest
{
private KlasController Controller;
private IOpdrachtRepository OpdrachtRepo;
//Use TestInitialize to run code before running each test
[TestInitialize()]
public void MyTestInitialize()
{
OpdrachtRepo = new DummyOpdrachtRepository();
Controller = new KlasController(new DummyKlasRepository(),
new DummyLesRepository(), OpdrachtRepo);
Opdracht TestOpdracht = new Opdracht
{
OpdrachtID = 1,
VakID = 1,
StamNummer = "im1"
};
Vak TestVak = new Vak { VakID = 1, VakNaam = "FOOP" };
TestOpdracht.Vak = TestVak;
OpdrachtRepo.addOpdracht(TestOpdracht);
}
/// <summary>
///A test for Index
///</summary>
[TestMethod()]
public void CreateKlasDirectsToToonKlassen()
{
Klas Klas = new Klas { KlasNaam = "2dNet" };
RedirectToRouteResult view = Controller.Create(1) as RedirectToRouteResult;
Assert.IsNotNull(view);
Assert.AreEqual("ToonKlassen", view.RouteValues["action"]);
}
}
at the moment I get a nullreferenceException on the view (assert.isNotNull fails)
and here is one of my DummyRepository's:
class DummyOpdrachtRepository : IOpdrachtRepository
{
List<Opdracht> opdrachten;
public DummyOpdrachtRepository()
{
opdrachten = new List<Opdracht>();
}
public void addOpdracht(Opdracht opdracht)
{
opdrachten.Add(opdracht);
}
public string GetDocentID(int OpdrachtID)
{
var opdracht = opdrachten.Where(o => o.OpdrachtID == OpdrachtID).FirstOrDefault();
return opdracht.StamNummer;
}
public Opdracht Find(int id)
{
return opdrachten.Where(o => o.OpdrachtID == id).FirstOrDefault();
}
}
Normally I should have written the tests Before writting the code, I know (and I am convinced off TDD, as I have used it successfully in my latest Java-project). but it just doesn't seem to work..
here is the code for KlasController.Create action
public ActionResult Create(int id) //id = opdrachtID
{
var Opdracht = OpdrachtRepo.Find(id);
Vak vak;
if(Opdracht != null)
vak = Opdracht.Vak;
else
throw new NullReferenceException("Deze opdracht werd niet gevonden");
return View(new CreateKlasModel(id,vak));
}
I know this is a lot of code, but I really want to make this work.
Thanks for helping me out in advance :)
As vladimir77 already says in his comment, the method public ActionResult Create(int id) is of type ViewResult, so either you change you method to do areturn RedirectToRoute() or you change your test to
ViewResult view = Controller.Create(1);
Assert.IsNotNull(view);
A ViewResult can not be cast as a RedirectToRouteResult.
Can someone tell me whey the following code blows when the moq.SetupProperty fails in the code below:
[TestMethod]
public void SimulatorService_Returns_HighScores()
{
IScoreService scoreService = new ScoreService(MockScoreRepository.GetMockScoreRepository());
Assert.IsNotNull(scoreService);
var highScores = scoreService.GetHighScores();
Assert.IsTrue(highScores.Count > 0);
}
public static class MockScoreRepository
{
public static ScoreEntry GetMockScoreEntry(int seed)
{
var moq = new Mock<ScoreEntry>();
moq.SetupProperty(s => s.UserID, seed);
moq.SetupProperty(s => s.Score, 10 * seed);
moq.SetupProperty(s => s.EntryDate, DateTime.Now);
return moq.Object;
}
public static IScoreRepository GetMockScoreRepository()
{
var scores = new List<ScoreEntry>();
for (var i = 0; i < 20; i++)
{
scores.Add(GetMockScoreEntry(i));
}
var repository = new Mock<IScoreRepository>();
repository.Setup(r => r.GetScores()).Returns(scores.AsQueryable());
return repository.Object;
}
}
The first thing I'd check is to make sure that the properties of ScoreEntry that you're setting have accessible setters. In other words, make sure your setters have public access or that you at least have a setter for each property you're attempting to set through moq.