from a backend controller, I need to make an HTTP GET request to an external service (KeyBase) to validate user input. What’s the library the Discourse uses to do this? I tried with Faraday, but I expect a json response and faraday_middleware is not an available gem.
I was able to get Faraday to work, but the tests are failing with a very strange error, it looks like Faraday is not able to make a request in the test. Is that possible? Is there something I am expected to do to be able to make external requests in tests?
EDIT: to be more clear: the request works fine in the actual page, but not in the tests.
We use https://github.com/bblimke/webmock so pretty much anything should work, if you simply need a simple HTTP get, this class has simple example of how we use FinalDestination class that wraps stuff for you.
For a simple get excon may be overkill. We have been slowly winding down on our excon use in core due to strangeness with it. I would recommend avoiding faraday we don’t ever use it in core.
Regarding getting stuck with specs. A huge time saver is
bin/rake autospec
Once you run that, save you plugin.rb file and right away all the specs will run, same goes for specific controller specs.
Thank you my struggle with this specific issue was that I couldn’t find a spec to learn from I found stub_request and tried to use it, but I think I got into one of the strange issues with Faraday you mentioned because the spec kept failing
One question: so I should keep bin/rake autospec running while I am working on the plug-in and it will automatically pick the changes and run the tests again?
Yes, there is also a fantastic trick I use I highly recommend.
Say I am working on something_spec.rb in the plugin I will edit in:
it "I am working on this spec" do
boom
end
Then I save it … and I am well focused on the broken specs. Then I improve the spec, leaving the “boom” at the end so autospec stays focused, saving other files and the spec file as I go. autospec stays focused and keeps running as needed.
It looks like bin/rake autospec is not checking plugin tests for me. If I use LOAD_PLUGINS=1 bin/rake autospec, then a ton of tests start failing (probably the ones related to other plugins).
I tried running bin/rake db:migrate to create the tables that the plugins need, but to no avail. Many tests keep failing…
So, I am running LOAD_PLUGINS=1 bin/rake autospec, but other specs fail, so my spec file is never picked. Is it normal that the first time it runs it takes more than 10 minutes?
This is the output:
Failures:
1) UsersController#perform_account_activation valid token welcome message enqueues a welcome message if the user object indicates so
Failure/Error: expect(Jobs::SendSystemMessage.jobs.size).to eq(1)
expected: 1
got: 0
(compared using ==)
# ./spec/requests/users_controller_spec.rb:34:in `block (5 levels) in <main>'
2) User.enqueue_welcome_message enqueues the system message
Failure/Error: Jobs.expects(:enqueue).with(:send_system_message, user_id: user.id, message_type: 'welcome_user')
Mocha::ExpectationError:
not all expectations were satisfied
unsatisfied expectations:
- expected exactly once, not yet invoked: Jobs.enqueue(:send_system_message, {:user_id => 3883, :message_type => "welcome_user"})
# ./spec/models/user_spec.rb:152:in `block (3 levels) in <main>'
In total, 8 specs are failing (in addition to mine)…
EDIT: an update. bin/rake autospec (without LOAD_PLUGINS=1) fails too, with 4 failures instead of 8.
@sam, @daniel, it turns out that autospec doesn’t work with symlinked directories in plugins/, so I just checked out my repo directly in plugins/ instead of symlinking it.