Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stub_responses function in Dynamodb, how can I pass query when stub? #3036

Closed
namdv-1375 opened this issue Jun 4, 2024 · 4 comments
Closed
Labels
documentation This is a problem with documentation. guidance Question that needs advice or information.

Comments

@namdv-1375
Copy link

namdv-1375 commented Jun 4, 2024

Describe the issue

I'm having a problem when my code uses get_item multiple times. However, if I write the test this way, every call to get_item will return the same data:

stub_dynamodb_client = Aws::DynamoDB::Client.new(stub_responses: true)
stub_dynamodb_client.stub_responses(:get_item, item: {"id" => 1})

How can i differentiate each time get_item to assign data to them differently? example:
first time: stub_dynamodb_client.stub_responses(:get_item, item: {"post_id" => 1})
second time: stub_dynamodb_client.stub_responses(:get_item, item: {"comment_id" => 2})
Pls, help me. Thankssss

Links

https://docs.aws.amazon.com/sdk-for-ruby/v2/api/Aws/ClientStubs.html#stub_responses-instance_method

@namdv-1375 namdv-1375 added documentation This is a problem with documentation. needs-triage This issue or PR still needs to be triaged. labels Jun 4, 2024
@jterapin
Copy link
Contributor

jterapin commented Jun 4, 2024

Hi! You could do that as follows:

stub_dynamodb_client.stub_responses(
   :get_item,
  [
    { item: {"post_id" => 1 } },
    { item: {"comment_id" => 2 } },
  ],
)

To demostrate via repl:

[13] pry(Aws)> stub_dynamodb_client.get_item(table_name: '', key: {})           
[Aws::DynamoDB::Client 200 0.000822 0 retries] get_item(table_name:"foo",key:{})  
=> #<struct Aws::DynamoDB::Types::GetItemOutput item={"post_id"=>0.1e1}, consumed_capacity=nil>
[14] pry(Aws)> 
[14] pry(Aws)> stub_dynamodb_client.get_item(table_name: '', key: {})           
[Aws::DynamoDB::Client 200 0.000596 0 retries] get_item(table_name:"foo",key:{})  
=> #<struct Aws::DynamoDB::Types::GetItemOutput item={"comment_id"=>0.2e1}, consumed_capacity=nil>

I noticed that the linked docs for V2. Here's a link to V3 incase you are using that version.

The last section in the documentation, noted as "Stubbing Multiple Responses" expands on the multiple stub responses so I suggest checking that out. Hope this helps!

@jterapin jterapin added guidance Question that needs advice or information. and removed needs-triage This issue or PR still needs to be triaged. labels Jun 4, 2024
@namdv-1375
Copy link
Author

@jterapin wow, thanks you <3

@jterapin jterapin closed this as completed Jun 4, 2024
Copy link

github-actions bot commented Jun 4, 2024

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

@mullermp
Copy link
Contributor

mullermp commented Jun 4, 2024

I believe also instead of an array you can pass multiple hash arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation This is a problem with documentation. guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

3 participants