I want to mock/stub:
#the_bill = GovKit::OpenCongress::Bill.find_by_idents("112-s368").first
for use in my tests.
which returns the following object that i would like to fix for the purpose of my tests:
--- !ruby/object:GovKit::OpenCongress::Bill
bill_type: s
co_sponsors:
- !ruby/object:GovKit::OpenCongress::Person {}
id: 68340
introduced: 1297836000
most_recent_actions:
- result:
created_at: "2011-02-17T07:45:50Z"
govtrack_order:
amendment_id:
text: Read twice and referred to the Committee on Agriculture, Nutrition, and Forestry.
date: 1297836000
how:
id: 287979
vote_type:
type: BillAction
roll_call_id:
action_type: action
datetime: "2011-02-16T00:00:00Z"
where:
bill_id: 68340
roll_call_number:
- result:
created_at: "2011-02-17T07:45:49Z"
govtrack_order:
amendment_id:
text:
date: 1297836000
how:
id: 287978
vote_type:
type: BillAction
roll_call_id:
action_type: introduced
datetime: "2011-02-16T00:00:00Z"
where:
bill_id: 68340
roll_call_number:
number: 368
plain_language_summary:
recent_blogs: []
I've tried Factory_girl (can't do it, not model based object), Fabrication (still same issues) and OpenStruct, probably possible, but had trouble converting yaml to OpenStruct and getting the mock in the right place.
Right now, i'm doing the api call in my tests, not what i want. I am thinking webmock is my solution, but I couldn't find out in the docs how to just load a simple object.
Try VCR for mocking out an API. I had the exact same question about 6 months ago and only recently discovered this library. It does exactly what you need, will cache the objects for testing later, but can also automatically refresh them on regular intervals. So far it's hands down the best solution I've found for this.
Related
I am experiencing a head-scratcher in a non-Rails app using AR that I cannot figure out. I will simplify greatly, but here is the essence: I have a Download object that belongs to a Ledger. In one of my unit tests, I am experiencing this:
dl = create(:download, account: checking)
dl.ledger
=> <Byr::Ledger:0x00007fd176ce4740
id: 2,
name: "test_ledger_1",
org_name: "Test Ledger, Inc.",
street1: nil,
street2: nil,
city: nil,
state: nil,
zip: nil,
created_at: 2022-04-03 13:13:53.734003153 UTC,
updated_at: 2022-04-03 13:13:53.792451592 UTC,
default_account_id: 21,
short_name: "Test_ledger_1",
parent_percent: nil,
parent_id: nil,
accessed_at: 2022-04-03 13:13:53.791911547 UTC,
start_date: Mon, 01 Jan 2018,
cost_method: "fifo",
end_date: nil
> dl.ledger.peristed? => true
> Ledger.find(2) => nil with eval error: Couldn't find Byr::Ledger with 'id'=2
I use factory_bot to create the Download, dl, which in turn builds a ledger for it to go with, which purports to be persisted with id=2. But when I try to find the ledger with Ledger.find(2), it's not in the postgresql db.
Anybody have any idea what might be going on here?
I think you should use something like faker + factory_bot and mock resources to get access to params you need.
Here an example of use. Hope this helps
I finally got this to pass by explicitly passing the ledger into the Account factory on which the Download factory was created, like this:
let(:ldg) { create(:ledger, name: 'Willy') }
let(:checking) { create(:bank_account, ledger: ldg) }
let(:dl) { create(:download, account: checking) }
Suddenly, the disappearing ledgers abated. I believe the cuplrit may have been DatabaseCleaner wiping away the automatically-generated ledger from one example to the next. Using an explicit ledger just for the example in question kept it from being deleted underneath my feet.
Thanks, #Joe, for giving this some thought.
For reference, I read:
Can't get SingleValueExtendedProperties from Outlook contacts for certain data types with GRAPH
Yes, I see that this thread is pretty old, but I hope you will still assist. So I was trying to execute a similar query:
https://graph.microsoft.com/v1.0/me/mailfolders/sentitems/messages/AAAB[truncated]BAAA=?$expand=singleValueExtendedProperties($filter=id%20eq%20'String%200x5D01001E')
This is just POC at the moment as I was noticing the String Comments by Marc in the refered article. This particular property is
Tag: 0x5D01001E
Type: PT_STRING8
Property Name: PR_SENDER_SMTP_ADDRESS_A
Other Names: PR_SENDER_SMTP_ADDRESS, PidTagSenderSmtpAddress, PR_SENDER_SMTP_ADDRESS_W, ptagSenderSMTPAddress
DASL: http://schemas.microsoft.com/mapi/proptag/0x5D01001E
I really would like the Binary property:
Tag: 0x00360003
Type: PT_LONG Property
Name: PR_SENSITIVITY Other
Names: PidTagSensitivity,ptagSensitivity
DASL: http://schemas.microsoft.com/mapi/proptag/0x00360003
When trying either, the query runs without error, but I do not get the MAPI Property. I switched to the PTString8 property PR_SENDER_SMTP_ADDRESS to prove I can get a String, but Eric's post in the referred document seem to make me think he could get a Non String. Both queries run, but no MAPI Property.
Any help would be welcomed. Thanks!
I figured it out! (Also, note that my first example PR_SENDER_SMTP_ADDRESS 0x5D01, is undefined in Sentitems Folder).
*Please Note:
0x0036 = PR_SENSITIVITY
https://graph.microsoft.com/v1.0/me/messages/{MessageID}?$expand=singleValueExtendedProperties($filter=id eq 'Integer 0x0036')
*Substitute the {MessageID} to be the Message Id of the Mail Message in question.
I'm developing on a game currently. It's a capitalism game of sorts, buying, selling, quests and such.
As I develop features, stores and items and things. I know I'm eventually going to want to put all this hardcoded data somewhere. In development, it's currently being fed into the database via db/seeds.rb. Is there a better place to put the data that I know will be static in production? Really the only data that is going to be changing dynamically is data having to do with users and join tables between users and the static data.
In past applications I've put a bit of static data in migrations, but this games static data will be significantly more than when I did that. Is there a best practice for this?
Here's some example data in db/seeds.rb that I think might have a better home:
pencil = Item.create(name: "pencil", value: 2, karma: 0)
lemon = Item.create(name: "lemon", value: 4, karma: 0)
pog = Item.create(name: "pog", value: 2, karma: 0)
child_store_items = [pencil, lemon, pog]
child_store = Store.create(name: "KB Toystore", karma: nil, min_age: 0)
child_store_items.each do |item|
StoreItem.create(store: child_store, item: item, quantity: 30)
end
school_quest = Quest.create(reward: 50, req_age: 0, req_time: 5, description: "learn all the things", title: "go to school")
school_quest_req = QuestItemRule.create(item: pencil, quantity: 3, quest: school_quest, rule: QuestItemRule.rules[:requirement])
Nothing wrong with having a config table in your database, but it's a bit overkill.
You can use a gem like railsconfig for application configuration. If you're talking about model configuration, where you have different constants or collections for different use cases, then I'd recommend creating a class for that whose methods return the values you need, itself optionally and/or partially setup with the help of a configuration gem. Depending on the nature of your static data, i18n might be useful here, too.
Perhaps you could store those things in YAML files...
Here is a YAML tutorial
In my Rails app I have a model called Cycle with a "start" attribute that is a date. I'm running into a very strange problem where sometimes Cycle.find_by_start will return the expected record, but at other times it will return nil.
For example Cycle.find_by_start("2011-05-01") returns the following:
=> #<Cycle id: 45, created_at: "2011-05-15 22:38:35",
updated_at: "2011-05-15 22:38:35", user_id: 20,
start: "2011-05-01", ending: nil, startguess: false, endingguess: nil>
But running Cycle.find_by_start("2011-05-13") returns nil, even though there is a record with a matching start value. I've verified that the record exists and the start value matches by running the following at the Rails console.
irb(main):012:0> Cycle.find(47)
=> #<Cycle id: 47, created_at: "2011-05-23 01:28:59",
updated_at: "2011-06-21 00:38:34", user_id: 12,
start: "2011-05-13", ending: "2011-05-31", startguess: false, endingguess: false>
irb(main):011:0> Cycle.find(47).start == "2011-05-13".to_date
=> true
Possibly relevant info: Running Rails 3.0.7 in development mode with an SQLite database.
Any ideas or troubleshooting tips?
Edit 1
Log of the SQL queries used:
[94m19:10:11 active_record [37mCycle Load (1.0ms) SELECT "cycles".* FROM "cycles" WHERE "cycles"."start" = '2011-05-01' LIMIT 1
[94m19:10:19 active_record [37mCycle Load (0.0ms) SELECT "cycles".* FROM "cycles" WHERE "cycles"."start" = '2011-05-13' LIMIT 1
Dates.... you may have a parsing problem, with US/UK formats getting swapped around and confusing things. I often find it helps to make the date unambiguous (assuming English months):
Cycle.find_by_start("13 May 2011")
If :start is a date field, then it's best to pass find_by_start an actual Date object rather than a string. So:
Cycle.find_by_start(Date.parse("2011-05-13"))
(I'm using Date.parse to create the date object here, but you could also use Date.new or Date.today or some other method)
Passing a string to the finder method might work but, as you've discovered, might also not - depending on the database type and how the database interprets the string.
I am using Ruby on Rails 3 and I would like to solve a issue counting ActiveRecord instances in an array.
I have this code
data = Account.where({:name => "Test_name", :city => "Test_city"}).limit(10)
The data debug is
#<Account:0x000001029d2da0>#<Account:0x000001029d2c60>#<Account:0x000001029d2bc0>#<Account:0x000001029d2b20>
The data inspecting is
"[#<Account name: \"Test_name\", city: \"Test_city\">, #<Account … >, #<Account id… >, …]"
Doubt: The ##<...> should be something like #<Account...>,#<Account...>,<...> (note commas)?
If in my code I use the following
data_count = data.count
The data_count is
nil
Why is it nil? How should I count accounts?
If I use result = data.class the debug of result is nil, but if I use result = data.classthe debug is "{\"inheritable_attributes\":{}}".
If I use Account.find_by_name("Test_name") instead of Account.where(...) I get same results as above.
To get to the bottom of things, start the rails console with:
$ rails c
Given that Account is an ActiveRecord model, you should be able to do the following in the rails console:
> Account.all.count
=> 100
> Account.where(:status=>'active')
=> [ #<Account id: 1, name: "a1", ...>, #<Account id: 2, name: "a2", ...>, #<Account id: 3, name: "a3", ...>, ...]
I'm doing a lot of hand waving here with ... since I don't know your schema. Replace the where condition with whatever works for your situation. The returned value should look like an array with a list of all the rows in the database that match the condition. BTW, an array is a list of element, and inspect (as well as the default display in the console) show element separated by commas. I haven't used debug so I can't comment on what it should do.
You can verify that the returned value is an AREL, and should be able to do some other operations to verify things work as expected.
> Account.where(:status=>'active').class
=> ActiveRecord::Relation
> Account.where(:status=>'active').size
=> 99
> Account.where(:status=>'active').count
=> 99
> Account.where(:status=>'active').limit(10).count
=> 10
If these work as expected in the console, there may be something in the view that is obscuring the correct behavior. In that case you'll need to post the details of your view code. If the strange behavior still occurs in the console, I would suggest posting the minimal parts of the actual model code that still exhibit the problem, along with the migration so we can see the schema.
I think you are having some problem in where condition.
Can you show the attributes value used in where clause.
For me its working fine:
data = Account.where('id != 0').limit(10)
data_count = data.count
Use the following:
data = Account.where("id = 2 and email = 'test_email#test.com'")