Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broaden changes to include factories? #78

Closed
nathany opened this issue Jul 8, 2014 · 5 comments
Closed

Broaden changes to include factories? #78

nathany opened this issue Jul 8, 2014 · 5 comments

Comments

@nathany
Copy link

nathany commented Jul 8, 2014

I came across a factory that needed to be updated:

-before(:create) {|a| a.stub(:attach_work_configuration) }
-after(:create) {|a| a.unstub(:attach_work_configuration) }
+before(:create) {|a| allow(a).to receive(:attach_work_configuration) }
+after(:create) {|a| allow(a).to receive(:attach_work_configuration).and_call_original }

Easy enough to handle manually or run transpec with a folder. Just thought I'd let you know.

# transpec 2.3.2
# ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]

transpec --convert example_group,hook_scope
@yujinakayama
Copy link
Owner

Did you really rewrite it to allow(a)to ... by hand and run it? Actually, in the factory_girl's after callback block, the self is an instance of FactoryGirl::SyntaxRunner so RSpec's allow is unavailable there (see Inconvertible Specs in the README).

Also, it's already warned by Transpec:

# spec/factories.rb
FactoryGirl.define do
  factory :book do
    after(:create) do |book|
      book.stub(:foo)
    end
  end
end
$ transpec
...
Converting spec/factories.rb
Cannot convert #stub into #allow since #allow is not available in the context.
spec/factories.rb:5:      book.stub(:foo)

By the way, if you had an error while running transpec with the factory_girl's after callback, I've fixed it now and released 2.3.3. See #66 for the details.

@nathany
Copy link
Author

nathany commented Jul 8, 2014

Sorry, it looks like I failed to run the spec.

Is it even possible to include or extend RSpec::Matchers in the context of FactoryGirl?

I'd like to disable all the old syntax if possible.

@yujinakayama
Copy link
Owner

You can, by including RSpec::Mocks::ExampleMethods (allow is not in RSpec::Matchers) into FactoryGirl::SyntaxRunner:

# In spec_helper.rb or somewhere
FactoryGirl::SyntaxRunner.send(:include, RSpec::Mocks::ExampleMethods)

However, I think this isn't a well-mannered way since FactoryGirl::SyntaxRunner is marked as a private API.

Personally I don't use factory_girl much so I'm not sure that stubbing in the after(:create) callback is a right way and whether there's a good alternative way. If it's unavoidable, I think you should submit a feature request to factory_girl project.

@yujinakayama
Copy link
Owner

Closing since Transpec can do nothing with this.

@nathany
Copy link
Author

nathany commented Jul 9, 2014

Thanks @yujinakayama.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants