The easiest way that I can think to test this is to pass the criteria
into my Users service and see that I get the same users back as the
messages that I will send.
The problem with this approach is that I know I will do exactly that
in the component that will send the email. At which point, it seems
like I have a tautology (given that the test and the function are
doing the same thing as it relates to getting users).
The other option that I can think of is to write a manual query to
check the results. Beyond mere duplication of work, this query itself
could quickly become outdated (part of the reason why I have things
wrapped up in services instead of having ad hoc queries all over the
pace).
Any thoughts or suggestions?
Why?
You've got a test that confirms Users.getUsers works, right?
Your test for Mailings.getMailingMessages should confirm it returns an
appropriate array of structs for the known set of users - you don't
need to re-test the users stuff, because you've got a simple and
independant Users.getUsers test already.
(i.e. you're not so much testing the function itself, you're testing
the *use* of the function)
I wondered if that wouldn't be the answer. As it happens, I wrote
getUsers before I started unit testing so I don't have any yet. I do
plan to add some there in the future and I'm confident enough in the
present that it is working correctly.
I'm not sure I follow what is left to test for getMailingMessages()
then. I was going to have two tests that it was returning the correct
users, but that seems to be a redundancy so I am not sure what else I
need to test there (except, perhaps, that the structure has the
correct keys, but that seems a bit too granular to me).
Am I missing something obvious? (it wouldn't be the first time, I'm sure)
Thanks,
Steve
OK, so what is getMailingMessages() supposed to do? What is its
purpose? What exactly are you testing?
> I also have a Users server (Users.cfc) with a getUsers() method that
> returns a query of users based on given criteria.
>
> I want to use Users.getUsers() to check the users that are returned by
> Mailings.getMailingMessages(). The problem with this is that I know
> that Mailings.getMailingMessages() will use Users.getUsers()
> internally.
>
I think perhaps that's where the problem lies. Because you're trying
to test Mailings.getMailingMessages(), but it is using
Users.getUsers(), you need to mock Users, otherwise you're doing an
integration test. It seems like you need to mock Users and have
getUsers() return some data in whatever format getUsers() actually
returns data. You will create that data in your test, so it will
always be the same, which makes it easy to do your asserts. Your test
will then call getMailingMessages(), and you can then check that the
results look the way they're supposed to, based on the mocked input
from getUsers().
--
Bob Silverberg
www.silverwareconsulting.com
That is a great question! I hadn't considered it that way, but it
really does seem like the right way to think about it.
I don't have an immediate answer for the question itself, but I think
you have set me up with my answer. As soon as I can answer that
question, then I think I am set to know what tests to write.
I just want to add that I am really excited about this line of
thought. It isn't a perspective I had taken before exactly, but I
really see a lot of value in it.
Thanks!
Steve