telegram-bot icon indicating copy to clipboard operation
telegram-bot copied to clipboard

How to rspec a incoming voice message

Open thomaswitt opened this issue 1 year ago • 1 comments

@printercu I am trying since a while to write a working rspec test for an incoming voice message:

      it "processes the voice message" do
        dispatch_message(nil, {
          "voice" => { "file_id" => "file123" },
          "from" => from,
          "chat" => { "id" => 123 },
        })
        messages = bot.requests[:sendMessage].map { |req| req[:text] }

Same goes for unsupported types - basically everything which is not just a plain message:

  describe "#unsupported_payload_type" do
    subject {
      -> {
        dispatch({
          "unsupported_type" => true,
          "message" => {
            "chat" => { "id" => 123 },
            "from" => { "id" => 123, "username" => "testuser" },
          },
        })
      }
    }

    before do
      allow(I18n).to receive(:t).with("telegram.errors.unsupported_type").and_return("Unsupported type")
      allow(I18n).to receive(:t).with("telegram.errors.general").and_return("An error occurred. Try again later.")
    end

    it "responds with an unsupported type message" do
      subject.call

      expect(bot.requests[:sendMessage].last[:text]).to eq("Unsupported type")
    end
  end

That doesn't seem to work correctly. Any ideas/Hints?

thomaswitt avatar Oct 15 '24 09:10 thomaswitt

What issue do you have with voice message?

Regarding unsupported type: this update is invalid. Update must have only one item, but in your examples there are unsupported_type and message.

printercu avatar Oct 18 '24 20:10 printercu

@printercu Fixed it, correct code:

    subject {
      -> {
        dispatch({
          'unknown_update_type' => {
            'from' => { 'id' => 123, 'username' => 'testuser' },
            'chat' => { 'id' => 123 },
            # Include any other necessary fields
          },
        })
      }
    }

Thanks for the help!

thomaswitt avatar Nov 08 '24 09:11 thomaswitt