Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

Ruby on Rails - Write Rspec tests for an abstract class

I'm working on a project that has required me to update the typical functionality of ApplicationRecord to include a validation to check that every model included in the DB has a particular field. I've set this up in the following way:

class MyCustomApplicationRecordClass < ApplicationRecord
    self.abstract_class = true
    validate :custom_validation


    def custom_validation
        ...
    end
end

My models then inherit from this class in the following way:

class MyModel < MyCustomApplicationRecordClass
...
end

While the validation works as anticipated & I can write tests confirming the functionality is working for each of the classes that inherit from MyCustomApplicationRecordClass, I'm struggling with how to write tests confirming that the validation on this class is behaving as anticipated. I've tried creating a "test" class, but receive an error that my test class doesn't correspond to a table in my db (which of course it doesn't'). What's the best way to test that this validation is performing as anticipated?

question from:https://stackoverflow.com/questions/65928880/ruby-on-rails-write-rspec-tests-for-an-abstract-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...