Rails test giving me an error of "undefined method 'body' " - ruby-on-rails

I am writing tests for my rails application. I wrote tests when I finished the user model for my project. The tests were working fine when I wrote them a few months back, however, I am getting the same error on 15 different tests. The errors are given below.
1) Error:
UserTest#test_email_should_be_present:
NoMethodError: undefined method `body' for #<User:0x00000008c84238>
test/models/user_test.rb:21:in `block in <class:UserTest>'
2) Error:
UserTest#test_should_be_valid:
NoMethodError: undefined method `body' for #<User:0x00000008caebf0>
test/models/user_test.rb:11:in `block in <class:UserTest>'
3) Error:
UserTest#test_name_should_not_be_too_long:
NoMethodError: undefined method `body' for #<User:0x00000008cb5068>
test/models/user_test.rb:26:in `block in <class:UserTest>'
4) Error:
UserTest#test_associated_scoreboards_should_be_destroyed:
NoMethodError: undefined method `body' for #<User:0x00000008cc2650>
test/models/user_test.rb:69:in `block in <class:UserTest>'
5) Error:
UserTest#test_email_addresses_should_be_unique:
NoMethodError: undefined method `body' for #<User:0x00000008cce4f0>
test/models/user_test.rb:55:in `block in <class:UserTest>'
6) Error:
UserTest#test_email_validation_should_accept_valid_addresses:
NoMethodError: undefined method `body' for #<User:0x00000008cdf318>
test/models/user_test.rb:39:in `block (2 levels) in <class:UserTest>'
test/models/user_test.rb:37:in `each'
test/models/user_test.rb:37:in `block in <class:UserTest>'
7) Error:
UserTest#test_email_validation_should_reject_invalid_addresses:
NoMethodError: undefined method `body' for #<User:0x00000008ce4c28>
test/models/user_test.rb:48:in `block (2 levels) in <class:UserTest>'
test/models/user_test.rb:46:in `each'
test/models/user_test.rb:46:in `block in <class:UserTest>'
8) Error:
UserTest#test_email_should_not_be_too_long:
NoMethodError: undefined method `body' for #<User:0x00000008cf2508>
test/models/user_test.rb:31:in `block in <class:UserTest>'
9) Error:
UserTest#test_name_should_be_present:
NoMethodError: undefined method `body' for #<User:0x00000008cfff78>
test/models/user_test.rb:16:in `block in <class:UserTest>'
10) Error:
UserTest#test_password_should_have_a_minimum_length:
NoMethodError: undefined method `body' for #<User:0x00000008d05b08>
test/models/user_test.rb:61:in `block in <class:UserTest>'
11) Error:
PasswordResetsTest#test_passwords_resets:
NoMethodError: undefined method `body' for #<User:0x000000061021a0>
app/controllers/password_resets_controller.rb:29:in `update'
test/integration/password_resets_test.rb:50:in `block in <class:PasswordResetsTest>'
12) Error:
UsersEditTest#test_successful_edits_with_friendly_forwarding:
NoMethodError: undefined method `body' for #<User:0x000000085088b0>
app/controllers/users_controller.rb:44:in `update'
test/integration/users_edit_test.rb:28:in `block in <class:UsersEditTest>'
13) Error:
UsersEditTest#test_unsuccessful_edits:
NoMethodError: undefined method `body' for #<User:0x000000082a6798>
app/controllers/users_controller.rb:44:in `update'
test/integration/users_edit_test.rb:13:in `block in <class:UsersEditTest>'
14) Error:
UsersSignupTest#test_Invalid_sign-up_submission:
NoMethodError: undefined method `body' for #<User:0x000000063cb9b8>
app/controllers/users_controller.rb:29:in `create'
test/integration/users_signup_test.rb:12:in `block (2 levels) in <class:UsersSignupTest>'
test/integration/users_signup_test.rb:11:in `block in <class:UsersSignupTest>'
15) Error:
UsersSignupTest#test_valid_sign-up_information_with_account_activation:
NoMethodError: undefined method `body' for #<User:0x0000000600c340>
app/controllers/users_controller.rb:29:in `create'
test/integration/users_signup_test.rb:26:in `block (2 levels) in <class:UsersSignupTest>'
test/integration/users_signup_test.rb:25:in `block in <class:UsersSignupTest>'
The error I get is a NoMehtodError: undefined method 'body', I am sure the problem isn't in the code. There must be a common denominator associated with all these errors. I get ZERO failures in my tests. I am not sure where this error is coming from. I don't have a method body anywhere in the code. The fixtures are not seeding any data into the test application. The code for the first few tests is given below.
test "email should be present" do
#user.email = " "
assert_not #user.valid?
end
test "should be valid" do
assert #user.valid?
end
test "name should not be too long" do
#user.name = "a" * 51
assert_not #user.valid?
end
I didn't include all the test code for all the error for the sake of keeping it brief and to the point. I wasn't exactly sure what code to include to help solve the problem. If any additional code is required please feel free to ask. As always, any help would be greatly appreciated.
Thanks

When we generate a model by using rails generate or rails g scaffolding command, it will invoke tasks and create some related files for us. One of the files is test/fixtures/...yml.
For example:
one:
user: one
body: MyText
two:
user: two
body: MyText
In ActiveSupport::TestCase class at test/test_helper.rb, the setup all fixtures method fixtures :all is called when we run tests. We have to modify the fixtures to follow the model attributes. If there is a mismatch between a model and the fixture, it will raise an error. In this problem, I believe there is a mismatch between User model attributes in the table and user fixture in test/fixtures/users.yml. Otherwise, please check another autogenerated fixture. There might be a body: MyText in the .yml file.
Hope it will help someone who faces the same problem

Related

Testing Shrine gem return error undefined method `set_derivatives'

I am trying to test uploads through Shrine.
I have Minio and followed this tutorial https://shrinerb.com/docs/testing#test-data
Yet FactoryBot.lint returns the following error from the TestModule code provided in the link:
profilepic - undefined method `set_derivatives' for #<Shrine::Attacher:0x000055ee893dfe00 #file=#<Shrine::UploadedFile storage=:store id="8aa14e79342fde0077fd48f1035f56c3" metadata={"size"=>576152, "mime_type"=>"image/jpeg", "filename"=>"profile_pic.jpg"}>, #cache=:cache, #store=:store, #context={}, #previous=nil, #column_serializer=Shrine::Plugins::Column::JsonSerializer, #model_cache=true, #model=nil, #destroy_block=nil, #promote_block=nil, #errors=[]> (NoMethodError)
# /home/maxence/.rvm/gems/ruby-3.0.3/gems/factory_bot-6.2.1/lib/factory_bot/linter.rb:13:in `lint!'
# /home/maxence/.rvm/gems/ruby-3.0.3/gems/factory_bot-6.2.1/lib/factory_bot.rb:70:in `lint'
# ./spec/support/factory_bot.rb:7:in `block (2 levels) in <top (required)>'
Any idea what's wrong ?
Just from the error it looks like derivatives plugin is not loaded. https://shrinerb.com/docs/plugins/derivatives
Add this to the test module
Shrine.plugin :derivatives

Undefined method map on writing to a CSV file in Ruby

I am trying to write lines from an input form to a CSV file from a string in a Rails model:
def form_to_csv
file = CSV.open('temp.csv', 'w+') do |csv|
self.input_data.split("\n").each do |line|
csv << line
end
end
input_data_file = InputDataFile.new(file: file)
self.input_data_file = input_data_file
end
However I get the error:
undefined method `map' for "...":String Did you mean? tap
Full error:
[FATAL] NoMethodError (undefined method `map' for "one\r":String Did you mean? tap):
[FATAL] app/models/job_request.rb:112:in `block (2 levels) in form_to_csv'
[FATAL] app/models/job_request.rb:111:in `each'
[FATAL] app/models/job_request.rb:111:in `block in form_to_csv'
[FATAL] app/models/job_request.rb:110:in `form_to_csv'
[FATAL] app/controllers/job_requests_controller.rb:21:in `create'
Any ideas welcome.
It looks like input_data is a method that calls map on a string.
If you're not sure what's going on, you could add a pry or byebug in app/models/job_request.rb:111, then run the form_to_csv method so it pauses execution at the point of error.
I'll guess you would be surprised at the value/datatype of a certain variable ...

NoMethodError: undefined method `create_reset_digest' for #<User:0x007fba32e0af58>

I got these 5 errors upon doing a bundle rake exec test. I have no idea how to resolve it. I've googled and googled with no end in site (see what I did there). Now I turn to you for help!
If you accept, these are the errors:
1) Error:
PasswordResetsTest#test_password_resets:
NoMethodError: undefined method `create_reset_digest' for #<User:0x007fba32e0af58>
app/controllers/password_resets_controller.rb:12:in `create'
test/integration/password_resets_test.rb:17:in `block in <class:PasswordResetsTest>'
2) Error:
UsersLoginTest#test_login_with_valid_information_followed_by_logout:
ActionView::Template::Error: undefined method `current_user?' for #<#<Class:0x007fba2cdf19a0>:0x007fba28cd32c0>
app/views/microposts/_micropost.html.erb:10:in `_app_views_microposts__micropost_html_erb___3978616761423832463_70218762096200'
app/views/users/show.html.erb:15:in `_app_views_users_show_html_erb___2170813965465965459_70218768032700'
test/integration/users_login_test.rb:14:in `block in <class:UsersLoginTest>'
3) Error:
UsersProfileTest#test_profile_display:
ActionView::Template::Error: undefined method `current_user?' for #<#<Class:0x007fba2cdf19a0>:0x007fba2f8c51e0>
app/views/microposts/_micropost.html.erb:10:in `_app_views_microposts__micropost_html_erb___3978616761423832463_70218762096200'
app/views/users/show.html.erb:15:in `_app_views_users_show_html_erb___2170813965465965459_70218768032700'
test/integration/users_profile_test.rb:11:in `block in <class:UsersProfileTest>'
4) Error:
UsersIndexTest#test_index_as_admin_including_pagination_and_delete_links:
ActionView::Template::Error: undefined method `current_user?' for #<#<Class:0x007fba2cdf19a0>:0x007fba28ed08e8>
app/views/users/_user.html.erb:4:in `_app_views_users__user_html_erb__3013593760514997339_70218798383040'
app/views/users/index.html.erb:7:in `_app_views_users_index_html_erb___1851646122892737362_70218763684120'
test/integration/users_index_test.rb:12:in `block in <class:UsersIndexTest>'
5) Error:
MicropostsInterfaceTest#test_micropost_interface:
ActionView::Template::Error: undefined method `current_user?' for #<#<Class:0x007fba312ac360>:0x007fba2ff66120>
app/views/microposts/_micropost.html.erb:10:in `_app_views_microposts__micropost_html_erb___3978616761423832463_70218762096200'
app/views/shared/_feed.html.erb:3:in `_app_views_shared__feed_html_erb___1999510692289199836_70218832039180'
app/views/static_pages/home.html.erb:13:in `_app_views_static_pages_home_html_erb__315324690869897794_70218832802580'
test/integration/microposts_interface_test.rb:11:in `block in <class:MicropostsInterfaceTest>'
BACKSTORY: I ignored these errors messages as I went through a few of the chapters. I'm not sure if it popped up between 9, 10, or 11. I am new to programming so I don't know if this is enough information for you to be able to help me. The deeper I get into programming material the more it all seems confusing. SOS
I suppose you are following the https://www.railstutorial.org
You have a recurring undefined method 'current_user' error in [2, 3, 4, 5]
Did you defined the current_user helper method as it should be done in your session_helper.rb ?

How to stub using any_instance in Rspec feature spec with Capybara?

I have the following in a feature spec:
it "shows the page" do
Project.any_instance.stub(:price_all)
login_user
gso = create(:gs_option)
gso.gs_collector.collector.project.update(user_id: #user.id)
visit edit_gs_option_path(gso)
end
Yet, it always fails because the price_all method on Project is not being stubbed. The failure trace contains the following:
# ./app/models/project.rb:430:in `price_all'
How do I stub the price_all method on the Project class?
I've tried stub(:price_all).with(anything()) and stub(:price_all).with(any_args()), but it doesn't change the failure message.
Here's the full failure:
1) GS Options page shows the page
Failure/Error: visit edit_gs_option_path(gso)
NoMethodError:
undefined method `id' for nil:NilClass
# ./app/models/collector.rb:435:in `price_item'
# ./app/models/gs_collector.rb:279:in `price_out'
# ./app/models/collector.rb:260:in `price_out_all'
# ./app/models/project.rb:430:in `price_all'
# ./app/controllers/application_controller.rb:187:in `get_totals'
# ./app/controllers/gs_options_controller.rb:6:in `edit'
# ./spec/features/gs_options_spec.rb:10:in `block (2 levels) in <top (required)>'
This stubbing feature started working when I upgraded to rspec-rails 3.0.0.beta.

config.action_controller.asset_path_template -- undefined method?

I'm trying to use:
https://github.com/polleverywhere/asset_fingerprinter
To install for Rails 3 you need to set:
config.action_controller.asset_path_template = AssetFingerprinter.path_rewriter
But that errors with:
.rvm/gems/ruby-1.9.2-p180#andyw/gems/actionpack-3.0.5/lib/action_controller/railtie.rb:54:in `block (3 levels) in <class:Railtie>': undefined method `asset_path_template=' for ActionController::Base:Class (NoMethodError)
Any ideas?
Thanks
I believe asset_path_template is not a method but a member in a hash
Try config.action_controller["asset_path_template"]

Resources