moq returning dataReader - moq

I'm having a strange experience with moq/mocking.
Im trying to mock the data going into a method so that i dont have to have adatabase available at test time.
So im loading in some data ive previously seralised.
Loading it into a dataTable, then creating a data reader from there, because my business layer method expects a data reader.
Then creating a mock for my data layer. and setting the return value for a particular method to my new datareader.
I am then setting (injecting) my mock data layer into my business layer so it can do the work of returning the data when the time comes..
var dataTable = DataSerialisation.GetDataTable("C:\\data.xml");
IDataReader reader = dataTable.CreateDataReader();
var mock = new Mock<IRetailerDal>();
mock.Setup(x => x.ReadRetailerDetails("00")).Returns(reader);
retailersBusinessLayer.RetailerDal = mock.Object;
var r = retailersBusinessLayer.GetRetailerDetail("00");
Now.. when the "GetRetailerDetail" is called is basically gets to "while(data.Read())" and crashes out but only sometimes.
I get the exception:
System.InvalidOperationException : DataTableReader is invalid for current DataTable 'Table1'.
Othertimes it move past that and can read some columns data, but other columns dont exist. (which must be to do with my serialisation method)

Well, this isnt exactly a satisfactory answer, but the code works now..
its similar to this.. in that no reason was found.
here
Anyway... as stated above the issue was ocuring inside my GetRetailerDetail method, where the code hits while(data.Read()) it throws the error..
The fix.. change the name of the data reader variable.. i.e. its was "data" and its now "data2".. thats all i changed.

Related

call ds_list from diffrent object error

I have created a textbox object and in its create event it creates list like
lines = ds_list_create();
in step event of textbox I use ds_list_add(lines, "line one");
and it works fine.
Now I have a diffrent object that try to call ds_list_add(Textbox.lines, "line from diff object");
but on running it gives error about var not set before reading. i tried also to change to global.list = ds_list_create(); and still same problems.
can someone explain how to call ds_list from diffrent object.
You can call ds_list_* functions right like you did. Just, you must make sure the data structures a function is referring to does actually exists.
I tried the following for testing purposes. Create two objects, objTextBox and objOther and set their events as follows.
For object objTextbox:
Create Event
list = ds_list_create();
Press 'Space' Event
var str = get_string("I'm objTextbox:","");
ds_list_add(list,str);
Draw Event
for (var i=0;i<ds_list_size(list);i++)
draw_text(10,10+15*i,string(ds_list_find_value(list,i)));
For object objOther:
Press 'Shift' Event:
var str = get_string("I'm objOther:","");
ds_list_add(objTextbox.list,str);
Now add them to a room, and make sure first object to be created is objTextbox, which is the one creating the ds_list. Run.
When dealing with data structures, always make sure they were created before working with them.

in Flex 3.2 Having troubles converting remote object result to specific object on client side in modules

in Flex 3.2 Having troubles converting remote object result to specific object on client side in modules.
For example I have VIPSAdmin module.
it has function
private function doResult(event:ResultEvent):void {
var data_:Array = ArrayUtil.toArray(event.result);
var result:ResultDTO = data_[0] as ResultDTO;
if(!result.isError()) {
trace(result.result);
vipsAdminDTO = result.result as VIPSAdmin;
compId= vipsAdminDTO.compId; // second time dying here
}
}
Function invoked when I get data from remote objet.
First time all great,when I unload this modeule and load it again:
data_[0] as ResultDTO;
Performs fine, but
vipsAdminDTO = result.result as VIPSAdmin;
vipsAdminDTO always null!
Even when
trace(result.result);
produces [object VIPSAdmin]
What a heck I missing here!? Looks like it just cannot do
result.result as VIPSAdmin;
even when trace and debug says it is instance of VIPSAdmin
I've figured out what is the problem, problem is that when I first instantiate something in module then in main app, somehow classes are not alined even that they are identical !
So solution is to make a fake instance in application class first, then if you use that same class to make an instance in module it will work!
I do it very simple in my main application class I just added:
VIPSAdmin;
This seems to create some sort of ghost instance, which I belie will be pickup by GC later, but will build tables of instances properly! Which solved my problem.
Not sure if this is appropriate solution ! but it sure works.

Inserting objects into database from Flex app not working

I am trying to make a mobile app that inserts information into a database and while I have it pulling information and I have created my services, when I use the standard create service that Flash Builder 4.6 automatically makes, it just does not insert anything at all into the database.
I have tried everything and I am now with the following code:
I first create a variable array with the values of the service call
....
protected var Coordinatelist2:Coordinatelist = new Coordinatelist();
I then created a function to fill in the information. The variables SesID, Lat and Long are declared when my button is pressed.
....
protected function createCoordinatelist(item:Coordinatelist):void
{
Coordinatelist2.SessionID = SesID;
Coordinatelist2.Latitude = Lat;
Coordinatelist2.Longitude = Long;
createCoordinatelistResult.token = coordinatelistService.createCoordinatelist(Coordinatelist2);
}
After this, I then go and add the following line of code to the end of my button function.
.....
createCoordinatelist(Coordinatelist2);
Now, as far as I am concerned, this should then be writing to the database the items of SesID, Lat and Long using the created service token, but when I do this, nothing has entered into the database at all.
Am I doing something wrong here?
Sorry for the confusion but I came right.
What I found I had not done was commit my token once I created it. I simply used createCoordinatelistResult.token = coordinatelistService.commit(); and it worked perfectly.
Thanks for the feedback

HTTPService not retrieving current data

I'm using mx.rpc.http.HTTPService to retrieve data from a web service. On the initial call to "loadWsData", HTTPservice accurately retrieves all the data.
However, on any and all subsequent calls HTTPService does not accurately retrieve the data; rather it always retrieves the first data set. I've confirmed that the web service is providing accurate data, both from web browsers and a ruby ws client script.
My code is below; any ideas on what could be the problem?
private function loadWsData(id:int):void
{
var webService:HTTPService = new HTTPService();
webService.url = "http://xxx.xxx.xxx.xxx:8080/profile/ + id;
webService.method = "GET";
webService.addEventListener(ResultEvent.RESULT, function(event:ResultEvent):void
{
var rawData:String = String(event.result);
var user:Object = JSON.decode(rawData).user; // User object always reflects the first data set retrieved.
....
....
});
webService.send();
}
Not sure what the issue might be, but I have a few suggestions on where to look.
First, there appears to be a bug in your code; the webService.url line is missing a quote mark. That could be messing up the URL you think you are sending. Odd, though, because I don't think what you have shown would compile, so I suspect this is just a cut-and-paste error when you posted this to StackOverflow, but I would trace out that URL just to be sure.
Also, I don't see code to remove the event listener (although it could be in the code you have abbreviated with ellipsis). Is it possible that there are lingering event listeners that are firing in addition to the ones you expect? If the original event listener fires, it will fire with the original data.
Another suggestion: instead of using a closure, try pulling it out to a separate function. That shouldn't be the issue, but maybe scope is playing a role here.
You could try to POST your results.
You might also add an event listener for FAULT, and see if there are any errors being thrown by your service request.

Silverlight RIA domain service. How do I simply get a the entity collection?

This should be REALLY simple but I can't figure it out.
I can bind the data to a datagrid like this below...
var context = new DishViewDomainContext();
this.dataGrid1.ItemsSource = context.Restaurants;
context.Load(context.GetRestaurantsQuery());
. That works... But now I just want that collection in a variable that I can loop through collection... This does not seem possible.. I do this and there seems to be nothing there... Im not really sure what the 3rd line does.. Its running the domain service method but where is it filling the data at?
var dc = new DomainService1();
IEnumerable<ApplicationLog> collApplicationLog = dc.ApplicationLogs;
dc.Load(dc.GetApplicationLogsQuery());
foreach (ApplicationLog al in collApplicationLog)
{
int? i = al.ApplicationID;
}
Try the following
var collApplicationLog = dc.ApplicationLogs.ToList();
The call to dc.Load(...) is an asynchronous network operation. The call returns immediately, but the data it's supposed to load won't be available until sometime later.
The return value of dc.Load(...) is a LoadOperation object, which has a Completed event. You need to add an event handler to the Completed event so that you will be notified when the data has been loaded and the IEnumerable can be used.
Here's why: Using just an IEnumerable you won't know when the data arrives. The EntitySet that you extract an IEnumerable from also implements INotifyCollectionChanged. That's how XAML databinding to the IEnumerable works - the data binding finds out when the data arrives because it listens to the collection changed notification, then it goes and fetches the data from the enumeration.
Your foreach loop isn't waiting for the data to arrive.

Resources