Search code examples
ruby-on-railsruby-on-rails-5acts-as-audited

Testing if model is audited without RSpec


I use audited gem to audit all changes on my models. In gem specification (https://www.rubydoc.info/github/collectiveidea/audited/Audited/RspecMatchers) we can find info how to test it with use of RSpec, i.e:

it { should be_audited }
it { should be_audited.associated_with(:user) }

I would like to do similar tests but without RSpec - any suggestions how to do it?


Solution

  • You can check if it respond_to? certain methods that come along with audited gem. like

    User.respond_to?(:audited)
    # or
    User.respond_to?(:audits)
    

    Also you could use auditing_enabled option from the readme.

    User.auditing_enabled
    

    This returns in default true when audited is there.

    The same for associated audits. Just build a data structure for you relation and check of the audits are equal your expected results. Take this example from the audited readme for example:

    company = Company.create!(name: "Collective Idea")
    user = company.users.create!(name: "Steve")
    user.update_attribute!(name: "Steve Richert")
    user.audits.last.associated # => #<Company name: "Collective Idea">
    company.associated_audits.last.auditable # => #<User name: "Steve Richert">
    

    There you can then easily check if the associated audits look like you want it.