Test DataAccess class build on WCF service - unit-testing

I put my NHibernate data access class in WCF service to can consume it by Silverlight project, but I have error and want to test my queries.
It is possible to test this queries in service class using NUnit ? Earlier I test normally this class but how do it when it is in service class ??
It's my WCF service class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using DataTransfer;
using NHibernate;
using NHibernate.Cfg;
using System.Diagnostics;
namespace WcfService1
{
public class Service1 : IService1
{
private ISession _session;
public Service1()
{
try
{
_session = (new Configuration()).Configure().BuildSessionFactory().OpenSession();
}
catch (Exception e)
{
Debug.Write(e);
throw;
}
}
public IList<Dziecko> GetChildByFirstname(string _firstname)
{
return _session.CreateCriteria(typeof(Dziecko))
.Add(NHibernate.Criterion.Expression.Eq("Imie", _firstname)).List<Dziecko>();
}
public IList<Dziecko> GetChildByLastname(string _lastname)
{
return _session.CreateCriteria(typeof(Dziecko))
.Add(NHibernate.Criterion.Expression.Eq("Nazwisko", _lastname)).List<Dziecko>();
}
public IList<Dziecko> GetChildByFirstnameAndLastname(string _firstname, string _lastname)
{
return _session.CreateCriteria(typeof(Dziecko))
.Add(NHibernate.Criterion.Expression.Eq("Imie", _firstname)).Add(NHibernate.Criterion.Expression.Eq("Nazwisko", _lastname)).List<Dziecko>();
}
}
}

If you want to test the queries themselves, I'd recommend putting them into a separate assembly (maybe using the repository pattern) and calling the methods in this assembly from your service. This will make it easier to test the queries themselves, and also allow you to mock the repositories when testing the service.

Related

UWP C# app calling Windows Runtime Component written in C++

I am trying to create a Windows Runtime(WinRT) Component written in C++(c++-cx) which can be called from my UWP C# app. I followed this MSDN Tutorial
It all went fine. I am able to build & run the code/sample specified in the above link on both Windows-10 Desktop & Mobile
I then tried to create a even simpler WinRT Component. I followed the same steps as above tutorial. Created a Windows Runtime Component project (File->New->Project->Visual C++ ->Windows->Universal->Windows Runtime Component(Universal Windows))
Now my class has only one function, getNum which basically returns a uint16
This is my Class header file
#pragma once
#include <collection.h>
namespace WRT_sample
{
public ref class Class1 sealed
{
public:
Class1();
uint16 getNum();
};
}
This is the corresponding cpp file:
#include "Class1.h"
using namespace WRT_sample;
using namespace Platform;
Class1::Class1()
{
}
uint16 Class1::getNum()
{
return 100;
}
I am using the same class names(Class1.h & Class1.cpp) which VS-2015 provides when I create a new WinRT project.
This is how I make a call to WinRT class from my C# code:
namespace CS_WRT_sample
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
initWRT();
}
void initWRT()
{
try
{
var nativeObj = new Class1();
var num = nativeObj.getNum();
this.Result2.Text = "Num from Cpp: " + num.ToString();
Debug.WriteLine("Num from Cpp: " + num);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
this.Result3.Text = ex.Message;
}
}
}
}
I am able to build & run my code on Desktop(x86/x64 Local Machine), but when I try to run on Mobile(x86 Emulator or ARM device), it fails with the following error:
Exception thrown: 'System.IO.FileNotFoundException' in CS_WRT_sample.exe
The specified module could not be found. (Exception from HRESULT: 0x8007007E)**
This is the stack trace which I see:
System.StubHelpers.StubHelpers.GetWinRTFactoryObject(IntPtr pCPCMD)
at WRT_sample.Class1..ctor()
at CS_WRT_sample.MainPage.initWRT()
My VS-2015 details:
MS Visual Studio Professional 2015
Version 14.0.25123.00 Update 2
MS .NET Framework
Version 4.6.01038
I am not sure, why I am hitting the issue, as I followed the same steps mentioned in the MSDN tutorial.
Any help will be highly appreciated.
You will run into this if you use a direct reference to a C++ winmd, instead of a P2P reference. This is easy to check in your .csproj:
For example, your .csproj should not have this line:
..\Debug\Abc_def\Abc_Def.winmd
Instead, consider using something like:
{1ee77824-eaa8-40f1-9b56-6f8ffa44e727}
Abc_Def

no overloaded function error while compiling web server client with gsoap

I am trying to use a Web service in my local. My server is written with C# and client c++ and gsoap. When I tried to compile my C++ code I am taking error below:
clientt.cpp
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xlocale(323) :
warning C4530: C++ exception handler used, but unwind semantics are not
enabled. Specify /EHsc
clientt.cpp(8) : error C2661: 'WebService1SoapProxy::HelloWorld' : no
overloaded function takes 1 arguments
I think, that function must take 1 argument. What's wrong with that?
Commands I used:
wsdl2h.exe -s -o calc.h calc.wsdl
soapcpp2.exe -i calc.h
cl client.cpp
.NET Web Service Server Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace webServiceDnm
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
C++ WEB Service Client Code:
/* CalcClient.cpp */
#include "soapWebService1SoapProxy.h"
#include "WebService1Soap.nsmap"
void main()
{
WebService1SoapProxy service;
char* result;
if (service.HelloWorld(result) == SOAP_OK)
std::cout<< result << std::endl;
else
service.soap_stream_fault(std::cerr);
}

for internal method test case method is not accessible

I am writing unit test case for my internal method. I have made necessary changes in AssemblyInfo.cs of my mail class project
[assembly: InternalsVisibleTo("myunitest_assemblyname")]
now i can access my internal method in unit test case method.
but when i compile the code it is giving an error as below
Error 1 'main class project name' does not contain a definition for 'Process' and no extension method 'Process' accepting a first argument of type 'main class project name' could be found (are you missing a using directive or an assembly reference?)
my main class has strong name.
it would be great if some one point where i am missing.
main class structure
namespace Renewals
{
public class StateProcessor
{
internal virtual void PutEmailInQueue(DataTable dataTable)
{
}
}
}
//test class structure
namespace Renewals.Tests.Unit
{
[TestClass]
public class StateProcessorTest
{
[TestMethod]
public void PutEmailInQueueTest()
{
DateTime processingDate = Convert.ToDateTime("27-feb-2013"); ;
StateProcessor stateProcess = new StateProcessor(processingDate);
stateProcess.PutEmailInQueue(new DataTable());
}
}
}
PutEmailInQueue - this method giving me problem.
you wrote that your class use strong name.
I think you have to modify your InternalsVisibleTo() statement with the public key.
e.g.: [assembly: InternalsVisibleTo("friend_signed_B, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e3aedce99b7e10823920206f8e46cd5558b4ec7345bd1a5b201ffe71660625dcb8f9a08687d881c8f65a0dcf042f81475d2e88f3e3e273c8311ee40f952db306c02fbfc5d8bc6ee1e924e6ec8fe8c01932e0648a0d3e5695134af3bb7fab370d3012d083fa6b83179dd3d031053f72fc1f7da8459140b0af5afc4d2804deccb6")]
for more informations see: http://msdn.microsoft.com/en-us/library/bb385180.aspx

How to add namespace in Unit Test Library(Windows store apps)?

I have some test class
using System;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using Praktyka.Models;
namespace PraktykaTest
{
[TestClass]
public class PictureManagerTest
{
[TestMethod]
public void LoadImagesTest()
{
var pic = new PictureManager();
pic.LoadImages(new List<String>
{
"1.jpg",
"2.jpg"
});
// Assert.AreEqual(#"dataImages\1.jpg",pic.Current().UriSource);
Assert.AreEqual("test","test");
}
}
}
I have compile error
The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?)
and
Cannot initialize object of type 'List<string>' with a collection initializer
How to add reference for correct working with Lists ?
First, add a reference to the System.Collections.Generic namespace to get the generic List<> class. Then try recompiling.

How to access a cli class in c++?

I need to make a new object of a cli class in plain C++ code.
I am new to cli, please help
my cli class:
using namespace System;
using namespace System::Windows::Forms;
using namespace CrystalDecisions::Shared;
using namespace CrystalDecisions::CrystalReports::Engine;
using namespace CrystalDecisions::Windows::Forms;
namespace CrystalRapport {
// This is the main form that will hold the viewer control.
ref class ViewForm : public System::Windows::Forms::Form
{
private:
//Declare the Viewer Control, Report Document, and other
//objects needed to set the connection information.
public:
ViewForm()..
void InitForm()..
//This function initializes the form and adds the viewer to the form.
void ViewForm_Load(System::Object^ sender, System::EventArgs^ e)..
};
}
You need to use gcnew if you want to create .NET object in CLI C++.
ref class Student
{
...
};
...
Student^ student = gcnew Student();
student->SelectSubject("Math", 97);
Ref:
http://www.codeproject.com/Articles/17787/C-CLI-in-Action-Instantiating-CLI-classes
I found an example. Use .NET Assemblies in Native C++ Applications
http://msdn.microsoft.com/en-us/vstudio/bb892742.aspx
If you create a C# Crystal Reports project you can use this example.
(with some changes)