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

Attempting to assign attributes sometimes results in assigned values being nil #1280

Closed
GlyphGryph opened this issue Apr 11, 2019 · 2 comments

Comments

@GlyphGryph
Copy link

I have a basic object in my application I use to import data from an external source. The factory I use for testing these objects looks like this:

factory :imported_object do
  sequence(:source_id) { |n| "id_#{n}" }
  source { 'default_source' }
end

Note: These are just plain text attributes. They are not a reference to some ruby object, or rails model. I have no factories or anything called source

Part of what I wanted to test was that the source_id attribute was required to be unique onlyfor particular sources, but I discovered that this does not work:

imported_object = build(:imported_object, source: '5', source_id: 'different_source')

Attempting to build (or create) that object results in

#<ScanResult id: nil, source_id: nil, source: nil %>

which is not terribly useful for my tests.

I suspect FactoryBot is trying to force these attributes to act like ActiveRecord associations, since rewriting my project to use "source_name" instead of "source" resolves the issue.

However, I probably shouldn't have to rewrite a project to start using FactoryBot, so hopefully there's something that could be done to fix this - or at the very least throw an error when you try to do it, saying FactoryBot doesn't support it (so I could have changed my product much sooner in this process)

I might look at this myself eventually, but it will probably take a week or two

@composerinteralia
Copy link
Collaborator

composerinteralia commented Apr 11, 2019

Thanks! I'm guessing this is a duplicate of #1142, which has been a common issue that we don't yet have a great solution to.

@composerinteralia
Copy link
Collaborator

Since we already have another issue to track this problem, I'm going to close this issue for now. If this turns out to be a different problem let me know and we can reopen the issue.

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