How to unit test asp.net api if using Application - asp.net-web-api

In my globals.asax.cs, I am creating a dictionary object that I want to use in my APIs to add and get data from in the life of the application.
I do the following:
Application["MyDictionary"] = myDictionary;
In my ApiController I get a handle of it, in this way:
MyDictionary myDictionary= (MyDictionary)HttpContext.Current.Application["MyDictionary"];
So far so good, the issue is that I need to do the same in the unit test, I need to add this dictionary into my Application as well, so when I call my controller, I will be able to retrieve it, how do I do that ?
doing this:
HttpContext.Current.Application.Add("MyDictionary", myDictionary);
enter code here
doesn't work, I get an exception:
An exception of type 'System.NullReferenceException' occurred in RESTServices.Tests.dll but was not handled in user code
My HttpContext.Current is null.
Any ideas how to work around this ?

The HttpContext.Current is created when a request is received, so you need to set the Current property, before using it.
See the example below:
var myDictionary = new Dictionary<string, string>();
HttpContext.Current = new HttpContext(new HttpRequest("default.aspx", "http://localhost", string.Empty), new HttpResponse(new StringWriter(CultureInfo.InvariantCulture)));
HttpContext.Current.Application.Add("MyDictionary", myDictionary);

Related

Microsoft Exchange Server: This Property was requested, but it was not returned by server (EWS)

I'm trying to get the value of a bool property Exchange Web Service "IsMeeting" using C#. I define the following properties in PropertySet object:
PropertySet propertySet = new PropertySet(ItemSchema.Subject,
AppointmentSchema.AppointmentState,
AppointmentSchema.Start,
AppointmentSchema.End,
AppointmentSchema.Organizer,
AppointmentSchema.IsMeeting,
AppointmentSchema.IsCancelled,
AppointmentSchema.IsRecurring,
AppointmentSchema.IsAllDayEvent,
AppointmentSchema.LegacyFreeBusyStatus,
ItemSchema.LastModifiedTime,
AppointmentSchema.TimeZone,
AppointmentSchema.MeetingRequestWasSent,
AppointmentSchema.ICalUid,
ItemSchema.Id);
However, when trying to get properties:
foreach (Appointment a in appointments)
{
Console.WriteLine(a.IsMeeting.ToString());
}
an exception occurs "ServiceObjectPropertyException". This Property was requested, but it was not returned by server.
A similar problem was with the IsCancelled property. Adding the AppointmentState property to the PropertySet helped.
With the IsMeeting property, there are no solution ideas. I will be glad if someone can tell me. Thanks.
Only a subset of properties are returned by FindItems because of performance reasons. If you really want that property then you need to make and additional GetItem (or Bind) request on the Item. If your using the EWS Managed API you can do that with LoadPropertiesForItems see https://learn.microsoft.com/en-us/dotnet/api/microsoft.exchange.webservices.data.exchangeservice.loadpropertiesforitems?view=exchange-ews-api&WT.mc_id=DOP-MVP-10145
You should also be able to just use AppointmentState which should return 0x0001 for a Meeting

Object reference not set to an instance of an object Xamarin.Forms using Acr.XamForms.UserDialog

In my app I fetch the data from the internet. While the data are being fetched I display a Loading dialog. For this I use Acr.XamForms.UserDiaologs library.
The code that I use is like below:
string Url = String.Format(category_url);
HttpClient hc = new HttpClient();
using (UserDialogs.Instance.Loading("Duke u ngarkuar..."))
contents = await hc.GetStringAsync(Url);
It works fine when I used in the inner pages of the app, but when I use in the first page it throws me the following error:
Object reference not set to an instance of an object
It must be something with a null reference value, have a look on the debugger to category_url, UserDialogs and double check if hc is being created correctly, step by step on your debugger.

AX2012 - Pre-Processed RecId parameter not found

I made a custom report in AX2012, to replace the WHS Shipping pick list. The custom report is RDP based. I have no trouble running it directly (with the parameters dialog), but when I try to use the controller (WHSPickListShippingController), I get an error saying "Pre-Processed RecId not found. Cannot process report. Indicates a development error."
The error is because in the class SrsReportProviderQueryBuilder (setArgs method), the map variable reportProviderParameters is empty. I have no idea why that is. The code in my Data provider runs okay. Here is my code for running the report :
WHSWorkId id = 'LAM-000052';
WHSPickListShippingController controller;
Args args;
WHSShipmentTable whsShipmentTable;
WHSWorkTable whsWorkTable;
clWHSPickListShippingContract contract; //My custom RDP Contract
whsShipmentTable = WHSShipmentTable::find(whsWorkTable.ShipmentId);
args = new Args(ssrsReportStr(WHSPickListShipping, Report));
args.record(whsShipmentTable);
args.parm(whsShipmentTable.LoadId);
contract = new clWHSPickListShippingContract();
controller = new WHSPickListShippingController();
controller.parmReportName(ssrsReportStr(WHSPickListShipping, Report));
controller.parmShowDialog(false);
controller.parmLoadFromSysLastValue(false);
controller.parmReportContract().parmRdpContract(contract);
controller.parmReportContract().parmRdpName(classStr(clWHSPickListShippingDP));
controller.parmReportContract().parmRdlContract().parmLanguageId(CompanyInfo::languageId());
controller.parmArgs(args);
controller.startOperation();
I don't know if I'm clear enough... But I've been looking for a fix for hours without success, so I thought I'd ask here. Is there a reason why this variable (which comes from the method parameter AifQueryBuilderArgs) would be empty?
I'm thinking your issue is with these lines (try removing):
controller.parmReportContract().parmRdpContract(contract);
controller.parmReportContract().parmRdpName(classStr(clWHSPickListShippingDP));
controller.parmReportContract().parmRdlContract().parmLanguageId(CompanyInfo::languageId());
The style I'd expect to see with your contract would be like this:
controller = new WHSPickListShippingController();
contract = controller.getDataContractObject();
contract.parmWhatever('ParametersHere');
controller.parmArgs(args);
And for the DataProvider clWHSPickListShippingDP, usually if a report is using a DataProvider, you don't manually set it, but the DP extends SRSReportDataProviderBase and has an attribute SRSReportParameterAttribute(...) decorating the class declaration in this style:
[SRSReportParameterAttribute(classstr(MyCustomContract))]
class MyCustomDP extends SRSReportDataProviderBase
{
// Vars
}
You are using controller.parmReportContract().parmRdpContract(contract); wrong, as this is more for run-time modifications. It's typically used for accessing the contract for preRunModifyContract overloads.
Build your CrossReference in a development environment then right click on \Classes\SrsReportDataContract\parmRdpContract and click Add-Ins>Cross-reference>Used By to see how that is generally used.
Ok, so now I feel very stupid for spending so much time on that error, when it's such a tiny thing...
The erronous line is that one :
controller.parmReportName(ssrsReportStr(WHSPickListShipping, Report));
Because WHSPickListShipping is the name of the AX report, but I renamed my custom report clWHSPickListShipping. What confused me was that my DataProvider class was executing as wanted.

Cast IPrincipal to IClaimsPrincipal returning null

I have inherited come code (an MVC web app) and am having trouble getting it to start.
These two lines exist:
var claimsPrincipal = principal as IClaimsPrincipal;
if (claimsPrincipal == null)
throw new ArgumentException("Cannot convert principal to IClaimsPrincipal.", "principal");
principal is an IPrincipal (in this case a System.Security.Principal.WindowsPrincipal), and is not null.
The first line sets claimsPrincipal to null, so the exception is thrown. I'm assuming it must have worked for someone at some point, and this is a fresh copy from source control. Why would this cast return null for me?
I see that this post is a long time ago. But I encountered the same problem today, and finally I get the way to resolve it.
In framework 4.5 or 4.6, you can directly cast System.Security.Principal.WindowsPrincipal into IClaimsPrincipal, because the first one implements the second one. But in framework 3.5 or 4.0, you cannot do this, becasue the first one doesn't implement the second one.It just implements IPrinciple, not IClaimsPrincipal. You can see it from MSDN link here.
Here it a way to resovle this and get the IClaimsPrincipal object.
var t = HttpContext.Current.User.Identity as WindowsIdentity;
WindowsClaimsPrincipal wcp = new WindowsClaimsPrincipal(t);
IClaimsPrincipal p = wcp as IClaimsPrincipal;
HttpContext.Current.User is a WindowsPrincipal, and finally you can get IClaimPrincipal.
principal might in fact be null. Did you debug that?
Check to see if the type of principal implements the IClaimsPrincipal interface.

MvcMailer: Can't complete NUnit tests on Razor Views which use Url.Action

Here's my problem - I am using MvcMailer to create nicely formatted emails using Razor syntax, and it's a great tool to use for that!
The issue I'm running into is this - here's some syntax from my View for one of the emails i send:
<p>Click here to return to #ViewBag.IdeaName</p>
Whenever I try to run my unit tests, I get the following error message:
Can we send out email notifications for new comments?: System.ArgumentNullException : Value cannot be null.
Parameter name: httpContext
Stacktrace - shortened for brevity, relevant sections only:
at System.Web.Routing.RouteCollection.GetRouteData(HttpContextBase httpContext)
at Mvc.Mailer.MailerBase.CreateControllerContext()
at Mvc.Mailer.MailerBase.ViewExists(String viewName, String masterName)
at Castle.Proxies.Invocations.MailerBase_ViewExists.InvokeMethodOnTarget()
at Castle.DynamicProxy.AbstractInvocation.Proceed()
The issue is that my HttpContext is null - is there a straightforward way to unit test this MvcMailer method without having to mock everything from the controller context all the way down the route results?
You can take a look at the section titled Unit Test Your Mailers at the MvcMailer wiki All you need to do is, just mock out the PopulateBody method and then it will bypass the view rendering as a part of the testing. It should look something like the following:
_userMailerMock.Setup(mailer => mailer.PopulateBody(It.IsAny<MailMessage>(), "Welcome", null));
Hope this helps!
This syntax worked for me:
var userMailerMock = new Mock<UserMailer> {CallBase = true};
userMailerMock.Setup(mailer => mailer.PopulateBody(It.IsAny<MailMessage>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Dictionary<string, string>>()));
You might want to mock the other overload as well (if the above doesn't help or just to be sure):
userMailerMock.Setup(mailer => mailer.PopulateBody(It.IsAny<MailMessage>(), It.IsAny<string>(), It.IsAny<Dictionary<string,string>>()));

Resources