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

Forced creating of an invalid record #578

Closed
shaggyone opened this issue Nov 12, 2013 · 8 comments
Closed

Forced creating of an invalid record #578

shaggyone opened this issue Nov 12, 2013 · 8 comments

Comments

@shaggyone
Copy link

Hello, is there any standart way to create an invalid record using factory girl.

First thing, of course is to use:
let(:var) { FactoryGirl.build :factory_name, ... }
before { var.save(validate: false) }

But maybe there is some pretty looking syntax for using FactoryGirl.create ?

@joshuaclayton
Copy link
Contributor

@shaggyone you can use custom methods to persist objects:

factory :different_orm_model do
  to_create {|instance| instance.save(validate: false) }
end

@waiting-for-dev
Copy link

But this way you always will save that factory objects skipping validations. It would be nice to have a way to define multiple creation methods, or simply be able to provide some argument to to_create. In my case, I only want to skip validations in some tests.

Thanks!

@servel333
Copy link

Does this work?

factory :user do
  factory :user_skips_validate do
    to_create {|instance| instance.save(validate: false) }
  end
end

FactoryGirl.create(:user, username: "valid_username")
FactoryGirl.create(:user_skips_validate, username: "invalid username -!@#$%^&*-=")

@waiting-for-dev
Copy link

Yeah, surely it works... but I usually like to have one factory per model, and just provide ways to have fined control over it through traits. But surely this is just my workflow and your proposal should be 100% acceptable. Thanks.

@thomassnielsen
Copy link

Using this technique inside traits seems to work.

trait :pay do
  # Turn off validation so we can create order with pay status directly
  to_create {|instance| instance.save(validate: false) }
  status "pay"
end

@aldrienht
Copy link

what about forcing to save record, column_validation not in model.
PG::NotNullViolation: ERROR: null value in column "not_null_column_here" violates not-null constraint

is it still possible?

@jferris
Copy link
Member

jferris commented Sep 6, 2018

@aldrienht You can't ignore or disable constraints in Postgres. If you really want to test with a nil value, you can use build_stubbed and mocks to simulate getting a nil result. However, given that it's impossible in your database, I can't think of why you'd want to model this in your tests.

@TorvaldsDB
Copy link

trait :skip_validate do
  to_create {|instance| instance.save(validate: false) }
end

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

8 participants