Advice on writing Ruby tests for plugins

One can write tests for the backend of a plugin. For example, I created the following file in my plugin directory:


require 'rails_helper'

describe MyPlugin::RouteStore do
  describe ".get_routes" do
    it "should return a Hash" do
      expect(described_class.get_routes().is_a?(Hash)).to be true

And then, I run the tests like this from Discourse’s root directory:

bundle exec rake plugin:spec["my-plugin-dir"]

/!\ Update: Should you get the error zsh: no matches found: plugin:spec[my-plugin-dir], that’s a known issue with zsh. You can workaround it by running the tests like this instead:

bundle exec rake "plugin:spec[my-plugin-dir]"
Old content before adding the update note

First of all, the following for running the tests don’t work although I see it recommended as the way to run the tests in the forum a lot:

  • bundle exec rake plugin:spec["disraptor"]: Errors with

    zsh: no matches found: plugin:spec[disraptor]
  • bundle exec rake plugin:spec disraptor: errors on a different plugin’s tests with:

    Failure/Error: class DetectedLink <, :is_quote); end
      superclass mismatch for class DetectedLink

Now, I have a general question regarding testability of certain code. The method I want to test in the code above looks like this:


def get_routes
  return PluginStore.get(MyPlugin::PLUGIN_NAME, 'key') || {}

Both parameters of PluginStore.get are hard-coded and currently cannot be changed from within the tests. This makes the method untestable because I would need to test actual plugin data from the store which I cannot rely on being there.

Discourse’s own tests for the discourse-narrative-bot plugin work around this by making the key parameter a parameter in the method they want to test (see plugins/discourse-narrative-bot/spec/discourse_narrative_bot/store_spec.rb for reference).

Are there other options for testing a plugin’s store?

1 Like

You need to use the directory name for you plugin here. So if it’s under /plugins/discourse-disraptor, then you run bundle exec rake plugin:spec[discourse-distraptor]

Why not just put the data in the database? Before testing the get_routes function, run this:

PluginStore.set(MyPlugin::PLUGIN_NAME, 'key', 'valuefortest')

The database is cleaned up automatically after each individual test. (tests run in a DB transaction, which is rolled back automatically)


I do. That’s the plugin’s name and directory name. That command errors as stated.

1 Like

Interesting… I guess it must be failing on this line

To confirm, your spec file is located at /plugins/disraptor/spec/lib/route_store_spec.rb?

How is your development environment configured? Windows / Mac / Linux?


That’s correct. However, disraptor is a symbolic link. Could that be the culprit? I’m using Ubuntu.

Also, note how the error that is produced there reads “No specs found.”, but I am getting “zsh: no matches found”.

1 Like

I think you’re right

Using symlinks for plugins in development seems like a pretty reasonable setup, so I went ahead and added support in


I just tried running bundle exec rake plugin:spec["poll"] and that produces the same error. However, the poll plugin is not symlinked, so that can’t be quite right, or can it?

The piece of code you linked to is also not executed when running bundle exec rake plugin:spec["poll"].

Comparing with the other command: bundle exec rake plugin:spec poll produces the same error in the unrelated plugin as stated in the original post.

1 Like

To be sure we’re looking at the correct part of the code, can you try running rails c and then

1 Like

That produces the correct output:

[1] pry(main)> Dir.glob("./plugins/poll/spec/**/*_spec.rb")
=> ["./plugins/poll/spec/lib/polls_validator_spec.rb",

Same for my plugin, by the way:

[2] pry(main)> Dir.glob("./plugins/disraptor/spec/**/*_spec.rb")
=> ["./plugins/disraptor/spec/lib/route_store_spec.rb"]
1 Like

How about

bundle exec rake "plugin:spec[poll]"

That should stop zsh trying to be clever


That’s it! I updated the original post to include this information. You might also want to revert the change in as traversal of symbolic links works already, right?

Regarding the testability question, I will clear the database in the tests which will not mess with the database from the development environment because all transactions will be rolled back.

1 Like

This should all be handled automatically. The test environment uses a separate database, and each test is run inside a transaction (which is never committed to the DB).


Ah, thank your for clearing that up. My RAILS_ENV was set to development, so the tests were running in development mode, too. After unsetting that, it uses the test environment by default. If one wants to enforce a test environment for the tests, they can run this:

RAILS_ENV=test bundle exec rake plugin:spec["my-plugin-dir"]

bin/rake autospec stops a lot of this fuffing around.

Simply run that … then save a spec file in your plugin.


FYI I fixed symlink traversal in bin/rake autospec today which was somewhat tricky but totally worth it.