Generator

14 views
Skip to first unread message

ignatandrei

unread,
Jul 23, 2009, 5:37:33 AM7/23/09
to NBuilder
I have seen NBuilder and I think it is great.

Could you add a generator ? Let me explain:
I have some objects that I load from database , like Manager and
Employee
class Manager has List<Employee> subordinates.
What I want to do is:
Step 1:
Manager m = Manager.LoadFromDatabase(); // load manager M1
m.LoadEmployeesSubordinates(); // let's say from database it has 3
employees
string s = NBuilder.ConstructCodeFor(m);

Step2
//Paste the code for s, that gives me something like:
var emp = Builder<Employees>.CreateListOfSize(3).WhereTheFirst(1).Have
(x=>x.Name="E1").And ...
Builder<Manager>.CreateNew().With(x=>x.Name="M1").subordinates.AddRange
(emp)

Thank you,

Ignat Andrei
Programmer free tools 2009
http://serviciipeweb.ro/iafblog/2009/04/08/MiniToolsList2009.aspx

garethdown44

unread,
Jul 23, 2009, 10:15:42 AM7/23/09
to NBuilder
Hi Ignat

So - basically you want to be able to generate the code that would
have to write if you were going to generate the same objects with
NBuilder? So in effect, it's kind of NBuilder in reverse - you start
off with a populated object and you end up with the code to build it.

It's an interesting idea but I think though it's really outside the
scope of what NBuilder is designed to do. I have a feeling something
like this would probably take a considerable amount of time to
implement, and I'm not really sure it would be possible. For instance
how would the generator know how to add an employee to a manager? It's
true in some cases it might be manager.Employees.Add() but personally
when I have a collection like that I would have a method
manager.AddEmployee(employee). The generator obviously wouldn't know
it had to do this.

Can I ask why you want to do this? Is it so you can recreate certain
scenarios using NBuilder for tests?

If that's the case it might be easier just to serialize the objects to
disk and then deserialize them in the tests?

Gareth

Ignat

unread,
Jul 23, 2009, 4:51:30 PM7/23/09
to NBuilder
The problem why - is because I have more application with EF/L2S - and
I have not find a simple way to load objects without recurse to
database. IoC is good - but does not function well with EF/L2S without
adding a lot of code ...
Many of my tests are directly involving the database. But there are
some tests, like verifying the creation of excel reporting documents,
that CAN not involve the database - however, the data must be
precisely the data from database, in order to have accurate report -
and not GIGO

I was thinking that NBuilder, because of generating data, will perform
the task.

garethdown44

unread,
Jul 30, 2009, 5:02:53 AM7/30/09
to NBuilder
Hi Ignat

I think I understand what you're saying.

The thing is though, I'm afraid this just doesn't fit in with the
"vision" of NBuilder. The purpose of NBuilder is to give developers an
easy way of rapidly creating new test data. It's not designed to
recreate data that already exists. So with this in mind, I don't think
this is a feature I can consider adding.

One thing I want to add is that NBuilder's default mode doesn't give
you "Garbage-In" unless you opt to use the random property namer.

For instance if I clear out my 'Products' table then with NBuilder I
do...

Builder<Product>.CreateListOfSize(10).Persist();

... I KNOW that the value of the 'Description' column in the 8th row
in the Products table is now going to be exactly "Description8". I
know that the Price is going to be 8.00 and I know the quantity will
be 8.

Therefore you should be able to write your test to check for these
known values.

If you have a database with loads of reference data, what I would do
is have a production backup of the database for the tests, then in
your test setup code, clear out the non reference data tables, e.g.

// test setup
Database.ClearNonStatic();

// arrange - (create test data)
Builder<Product>......

// act

// assert


This is how I go about things - I hope this helps in some way?


Gareth

Ignat

unread,
Jul 30, 2009, 6:25:20 AM7/30/09
to NBuilder
It is more clear. However, what I have in mind is clear the dependency
from database.
Let me explain :
I have TestA that fills some data from database, then performs some
modification and export data in CSV/XML/Excel. The verification is in
"hard" form - the CSV file generated must the same as generated first
time.

I want to have TestA reproduce if I fill the same data from
NBuilder ... but , thinking again, the task is more for a mock rather
then NBuilder.

However, I have used NBuilder for test purposes - and I am delighted !
Reply all
Reply to author
Forward
0 new messages