I am in the middle of refactoring out part of a ruby on rails codebase into a ruby gem. As part of the process, I have to shift some test cases over to the gem.
For one of the tests:
describe A::B do
describe "#someMethod" do
let(:user_hash) { {'id' => 3, "username" => "user", "email" => "[email protected]"} }
it "return the base64 message" do
described_class.message(user_hash).should ==
Base64.encode64(user_hash.to_json).gsub("\n", "")
end
The thing is, plain hashes do not have the to_json
method, so I keep getting a NoMethodError
when I run the tests. The to_json
method seems to be derived from activeresource
(https://github.com/rails/activeresource). How do I now augment the hash with the to_json
method?
Thank you.
When you encounter a NoMethodError
then it means that you don't have required libraries loaded. Add require 'json'
in your test case where you need to use the method to_json
.
To answer your question in your comment:
Do you know of a way to find out where such methods are actually from? I am using RubyMine to find out and it gives me this whole list of possible implementations
Really, search engines come to the rescue when you are looking for functionalities unknown to you. Probably not an appealing answer but that's how I learn :) While writing this answer I did this search on Google and the first result is the answer to your question.