Creating a custom build strategy that creates RSpec mock objects

159 views
Skip to first unread message

Andrew Meyer

unread,
Jul 18, 2013, 11:19:00 AM7/18/13
to factor...@googlegroups.com
I'm looking into using factory_girl with unit tests in my Rails application. I like to keep my unit tests isolated, so currently I'm using lots of RSpec mock objects in my tests. This has worked pretty well so far, but I'm beginning to suffer from some of the same problems that factory_girl seems to be intended to solve (i.e. I have to specify attributes that I don't actually care about in the tests, I have to update object initialization code all over when I change my object's attributes). My only problem is that factory_girl seems to only work with real objects, I can't use it to create mocks.

So what I want to do is create a custom build strategy kind of like build_stubbed that creates RSpec mock objects instead of real objects. In order to keep my tests isolated and not slow things down with unnecessary requires, I also want these factories to be able to function regardless of whether the real object they are mocking has been loaded yet. Is this possible?

Keenan Brock

unread,
Jul 19, 2013, 7:08:00 PM7/19/13
to factor...@googlegroups.com
Andrew,

You can specify the class name of the object you want to create.
I wonder if you can specify a mock.

I had used factory girl to create a hash for me.

Good luck,
Keenan

On Thursday, July 18, 2013 at 11:19 AM, Andrew Meyer wrote:

I'm looking into using factory_girl with unit tests in my Rails application. I like to keep my unit tests isolated, so currently I'm using lots of RSpec mock objects in my tests. This has worked pretty well so far, but I'm beginning to suffer from some of the same problems that factory_girl seems to be intended to solve (i.e. I have to specify attributes that I don't actually care about in the tests, I have to update object initialization code all over when I change my object's attributes). My only problem is that factory_girl seems to only work with real objects, I can't use it to create mocks.

So what I want to do is create a custom build strategy kind of like build_stubbed that creates RSpec mock objects instead of real objects. In order to keep my tests isolated and not slow things down with unnecessary requires, I also want these factories to be able to function regardless of whether the real object they are mocking has been loaded yet. Is this possible?

--
--
Individuals over processes. Interactions over tools. Agile Rails training from thoughtbot, the makers of Clearance, Shoulda, & Factory Girl:
http://thoughtbot.com/services/training
 
You received this message because you are subscribed to the "factory_girl" mailing list.
To post to this group, send email to factor...@googlegroups.com
To unsubscribe from this group, send email to
factory_girl...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/factory_girl?hl=en
---
You received this message because you are subscribed to the Google Groups "factory_girl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to factory_girl...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages