0

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.

3
  • 1
    Add require 'json' at the top. Commented Dec 11, 2013 at 7:19
  • hi vinodadhikary, that worked! Post it officially as the answer? 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 Commented Dec 11, 2013 at 7:21
  • please see my answer. I've tried to answer your question in your comment. Commented Dec 11, 2013 at 7:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Haha I see... looks like my googling skills can be improved. Thanks for your help!
If you use vim and Ctags, you can put the cursor on the method name and press <C-]> to follow the tag into the source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.