thoughtbot / thoughtbot/factory_bot

NoMethodError: undefined method 'to_sym' for CustomStrategy

Open
#1,536 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Ruby
Stars
8.2k
Forks
2.6k
PR merge metrics
No merged PRs in 30d

Description

Description

After upgrading from Version 6.2.0 to 6.2.1 our custom strategy was broken.
-> NoMethodError: undefined method 'to_sym' for CustomStrategy

This commit broke our code https://github.com/thoughtbot/factory_bot/commit/1b81d5dc258e5f1ad25c5109cb4bf3f6be83deb4

The change is not mentioned in the NEWS.rb nor in the GETTING_STARTED.md

https://www.rubydoc.info/gems/factory_bot/file/GETTING_STARTED.md#custom-strategies

Solution

If someone comes here with the same Problem it is super easy to fix. Just add the to_sym to your strategy. :)


Question

Could someone explain to me or point me in the direction where I can find more information why the custom strategies should be defined like this and not like the one underneath. There is no problem i'm just curious.

class JsonStrategy
  def initialize
    @strategy = FactoryBot.strategy_by_name(:create).new
  end

  delegate :association, to: :@strategy

  def result(evaluation)
    @strategy.result(evaluation).to_json
  end

  def to_sym
    :json
  end
end
class JsonStrategy < FactoryBot::Strategy::Create
  def result(evaluation)
    super.to_json
  end

  def to_sym
    :json
  end
end

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read commit 1b81d5dc258e5f1ad25c5109cb4bf3f6be83deb4, then compare its behavior with the custom strategies section in GETTING_STARTED.md and the entries in NEWS.rb. Done means the custom-strategy interface and the difference between the two examples are clearly documented, including the 6.2.1 breaking change.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
documentation, testing
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.