Rspec, fake class AR attribute
Hello i am trying to test method that in one place calls activerecord attribute, but however in test i created fake class to test it out and do not have a table for that test class, so how i could give that class a fake table or smthng?
tried to define load schema, but that didn't work:
class TestClassStatus < FrozenRecord::Base
end
class TestClass < ApplicationRecord
def self.load_schema!
@columns_hash="test_class_status_id" => @name=>'test_class_status_id', @table_name=>'TestClassStatus', @sql_type_metadata=> @sql_type=>'character varying', @type=>:string, @limit=>nil, @precision=>nil, @scale=>nil , @null=>false, @default=>'', @default_function=>nil, @collation=>nil, @comment=>nil, @max_identifier_length=>63
end
has_status
end
here i have TestClassStatus
where statuses from yml file are loaded by frozen record, and TestClass
is a class where i use the method i want to test has_status
. And that method is trying to find test_class_status_id
in TestClass
. But unfortunately i dont have a table for that fake TestClass
. So any advice how to make a fake table for that TestClass to run this spec? :)
Thank you in advance!
Here is a part where it breakes
name = status_name ? status_name.to_s : "#self.to_sStatus"
klass = name.camelize.constantize
include Module.new
class_eval %Q
def #association_name
#klass.find(self[:#name.underscore])
end
It brakes in line where it tries to find that attribute. klass in my case will be TestClassStatus
and name will be test_class_status
so in that line it tries to recieve test_class_status
attribute in TestClass
, but unfortunately as you can see my TestClass
does not have a table. So i need somehow to fake the table, because this method hits activerecord :)
There is a way to test it using real class, but I don't want to be attached to a real class in testing in case someday i would need to remove that class.
ruby activerecord
add a comment |
Hello i am trying to test method that in one place calls activerecord attribute, but however in test i created fake class to test it out and do not have a table for that test class, so how i could give that class a fake table or smthng?
tried to define load schema, but that didn't work:
class TestClassStatus < FrozenRecord::Base
end
class TestClass < ApplicationRecord
def self.load_schema!
@columns_hash="test_class_status_id" => @name=>'test_class_status_id', @table_name=>'TestClassStatus', @sql_type_metadata=> @sql_type=>'character varying', @type=>:string, @limit=>nil, @precision=>nil, @scale=>nil , @null=>false, @default=>'', @default_function=>nil, @collation=>nil, @comment=>nil, @max_identifier_length=>63
end
has_status
end
here i have TestClassStatus
where statuses from yml file are loaded by frozen record, and TestClass
is a class where i use the method i want to test has_status
. And that method is trying to find test_class_status_id
in TestClass
. But unfortunately i dont have a table for that fake TestClass
. So any advice how to make a fake table for that TestClass to run this spec? :)
Thank you in advance!
Here is a part where it breakes
name = status_name ? status_name.to_s : "#self.to_sStatus"
klass = name.camelize.constantize
include Module.new
class_eval %Q
def #association_name
#klass.find(self[:#name.underscore])
end
It brakes in line where it tries to find that attribute. klass in my case will be TestClassStatus
and name will be test_class_status
so in that line it tries to recieve test_class_status
attribute in TestClass
, but unfortunately as you can see my TestClass
does not have a table. So i need somehow to fake the table, because this method hits activerecord :)
There is a way to test it using real class, but I don't want to be attached to a real class in testing in case someday i would need to remove that class.
ruby activerecord
can you show us the test case and the error?
– max pleaner
Nov 15 '18 at 7:42
Added more context :)
– r2d
Nov 15 '18 at 13:09
why do you "need a table"? What is the actual test?
– engineersmnky
Nov 15 '18 at 19:15
beacuse as you can see method in included module hits active record, and i dont have that activerecord attribute in my fake class
– r2d
Nov 16 '18 at 9:07
add a comment |
Hello i am trying to test method that in one place calls activerecord attribute, but however in test i created fake class to test it out and do not have a table for that test class, so how i could give that class a fake table or smthng?
tried to define load schema, but that didn't work:
class TestClassStatus < FrozenRecord::Base
end
class TestClass < ApplicationRecord
def self.load_schema!
@columns_hash="test_class_status_id" => @name=>'test_class_status_id', @table_name=>'TestClassStatus', @sql_type_metadata=> @sql_type=>'character varying', @type=>:string, @limit=>nil, @precision=>nil, @scale=>nil , @null=>false, @default=>'', @default_function=>nil, @collation=>nil, @comment=>nil, @max_identifier_length=>63
end
has_status
end
here i have TestClassStatus
where statuses from yml file are loaded by frozen record, and TestClass
is a class where i use the method i want to test has_status
. And that method is trying to find test_class_status_id
in TestClass
. But unfortunately i dont have a table for that fake TestClass
. So any advice how to make a fake table for that TestClass to run this spec? :)
Thank you in advance!
Here is a part where it breakes
name = status_name ? status_name.to_s : "#self.to_sStatus"
klass = name.camelize.constantize
include Module.new
class_eval %Q
def #association_name
#klass.find(self[:#name.underscore])
end
It brakes in line where it tries to find that attribute. klass in my case will be TestClassStatus
and name will be test_class_status
so in that line it tries to recieve test_class_status
attribute in TestClass
, but unfortunately as you can see my TestClass
does not have a table. So i need somehow to fake the table, because this method hits activerecord :)
There is a way to test it using real class, but I don't want to be attached to a real class in testing in case someday i would need to remove that class.
ruby activerecord
Hello i am trying to test method that in one place calls activerecord attribute, but however in test i created fake class to test it out and do not have a table for that test class, so how i could give that class a fake table or smthng?
tried to define load schema, but that didn't work:
class TestClassStatus < FrozenRecord::Base
end
class TestClass < ApplicationRecord
def self.load_schema!
@columns_hash="test_class_status_id" => @name=>'test_class_status_id', @table_name=>'TestClassStatus', @sql_type_metadata=> @sql_type=>'character varying', @type=>:string, @limit=>nil, @precision=>nil, @scale=>nil , @null=>false, @default=>'', @default_function=>nil, @collation=>nil, @comment=>nil, @max_identifier_length=>63
end
has_status
end
here i have TestClassStatus
where statuses from yml file are loaded by frozen record, and TestClass
is a class where i use the method i want to test has_status
. And that method is trying to find test_class_status_id
in TestClass
. But unfortunately i dont have a table for that fake TestClass
. So any advice how to make a fake table for that TestClass to run this spec? :)
Thank you in advance!
Here is a part where it breakes
name = status_name ? status_name.to_s : "#self.to_sStatus"
klass = name.camelize.constantize
include Module.new
class_eval %Q
def #association_name
#klass.find(self[:#name.underscore])
end
It brakes in line where it tries to find that attribute. klass in my case will be TestClassStatus
and name will be test_class_status
so in that line it tries to recieve test_class_status
attribute in TestClass
, but unfortunately as you can see my TestClass
does not have a table. So i need somehow to fake the table, because this method hits activerecord :)
There is a way to test it using real class, but I don't want to be attached to a real class in testing in case someday i would need to remove that class.
ruby activerecord
ruby activerecord
edited Nov 15 '18 at 13:09
r2d
asked Nov 15 '18 at 5:37
r2dr2d
13
13
can you show us the test case and the error?
– max pleaner
Nov 15 '18 at 7:42
Added more context :)
– r2d
Nov 15 '18 at 13:09
why do you "need a table"? What is the actual test?
– engineersmnky
Nov 15 '18 at 19:15
beacuse as you can see method in included module hits active record, and i dont have that activerecord attribute in my fake class
– r2d
Nov 16 '18 at 9:07
add a comment |
can you show us the test case and the error?
– max pleaner
Nov 15 '18 at 7:42
Added more context :)
– r2d
Nov 15 '18 at 13:09
why do you "need a table"? What is the actual test?
– engineersmnky
Nov 15 '18 at 19:15
beacuse as you can see method in included module hits active record, and i dont have that activerecord attribute in my fake class
– r2d
Nov 16 '18 at 9:07
can you show us the test case and the error?
– max pleaner
Nov 15 '18 at 7:42
can you show us the test case and the error?
– max pleaner
Nov 15 '18 at 7:42
Added more context :)
– r2d
Nov 15 '18 at 13:09
Added more context :)
– r2d
Nov 15 '18 at 13:09
why do you "need a table"? What is the actual test?
– engineersmnky
Nov 15 '18 at 19:15
why do you "need a table"? What is the actual test?
– engineersmnky
Nov 15 '18 at 19:15
beacuse as you can see method in included module hits active record, and i dont have that activerecord attribute in my fake class
– r2d
Nov 16 '18 at 9:07
beacuse as you can see method in included module hits active record, and i dont have that activerecord attribute in my fake class
– r2d
Nov 16 '18 at 9:07
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313064%2frspec-fake-class-ar-attribute%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313064%2frspec-fake-class-ar-attribute%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
can you show us the test case and the error?
– max pleaner
Nov 15 '18 at 7:42
Added more context :)
– r2d
Nov 15 '18 at 13:09
why do you "need a table"? What is the actual test?
– engineersmnky
Nov 15 '18 at 19:15
beacuse as you can see method in included module hits active record, and i dont have that activerecord attribute in my fake class
– r2d
Nov 16 '18 at 9:07