ActsAsTenant current_tenant + Capybara + RSpec testing - ruby-on-rails-4

I am trying to create an integration test with Capybara/RSpec that has a valid current_user (using the Warden.test_mode! trick) and I have also created a valid current_tenant by doing this in the test:
ActsAsTenant.current_tenant = Account.first
When the test is ran it comes up with a "undefined method for nil:NilClass" error that is characteristic of the current_tenant not being set. I have verified that Account.first does in fact have what I expect in it.
What might be the problem and how can I fix it?

You can check the gem 'act_as_tent' doc over here
https://github.com/ErwinM/acts_as_tenant#testing

You might need to create the Account. If you're using FactoryBot, that might look like:
ActsAsTenant.current_tenant = FactoryBot.create :account
You might also consider using:
ActsAsTenant.without_tenant do
# more testy things
end

Related

How to update fixture column in Rails Minitest

How can i update column of a fixture for temporary use only with update_column command.
Right now i have following command that is running fine:
name = names(:one)
role = roles(:one)
name.role_id = role.id
assert name.save
And it is running fine, but is there any efficient way to do it in one line something like name.update_column(---, ----) ?
Thanks #richfisher for your answer, later on i figure out another way to do it. update_attributes is not a good idea to be used in test, because the problem with update_attributes is
It runs callbacks and validations
and usually we do not want to run these things in test cases
Instead of update_attributes we can use update_column like this
name.update_column(:role_id, roles(:one).id)
The advantage of using update_column is
It did not run callbacks and validations
name = names(:one)
name.update_attributes(role_id: roles(:one).id)

Spree and unit testing

I've been trying to run some unit tests in my spree application, which involves creating a new Order. The first hurdle I ran into had to do with countries not loading, due to seed data not being entered in the test database. A question was posted about it here, if you want extra credit work: https://github.com/spree/spree/issues/5308
However, I was able to bypass that issue by inventing a country inside the test, for the sake of testing the rest of my code. I've tried doing the same for a variant, but I keep running into this error:
Error:
VariantTest#test_variant_test:
RuntimeError: No master variant found to infer price
test/models/variant_test.rb:10:in `block in <class:VariantTest>'
I created a second test to see if Variants were getting made at all, and I got the same error message. This is the test I've run:
require 'test_helper'
class VariantTest < ActiveSupport::TestCase
test "variant test" do
f = Spree::Variant.new
f.cost_price = 20
f.sku = "test"
f.is_master = true
f.track_inventory = false
f.save!
test1 = Spree::Variant.find_by sku: "test"
assert_not_nil(test1, "Variant wasn't created")
end
end
I've tried creating two Variants, one of which is master and one of which is not, and testing the sku for the non-master variant, but I keep getting the exact same error message about the master variant not being found. Am I missing something?
Just to answer your queestion: You need to set price for your variant. You will get then new error because you're missing product for that variant and so on.
Belive me, you really want to use default factories with FactoryGirl, you won't have to lose time reinventing the wheel. Just look at them here or directly at variant factory, if you'll have any questions about them just ask.

New rails 4.1 mailer preview action not found error

Rails has 607 open issues...so, rather than plugging that hole even more I though Id try here first. I have upgraded to 4.1 and am implementing rails mailer previews. I tried upgrading my existing mailer and adding tests/mailers/previews directory. When that gave the following error I tried generating a new mailer. Same error.
class NotifierPreview < ActionMailer::Preview
def welcome
Notifier.welcome(User.first)
end
end
results in this error:
The action 'notifier' could not be found for Rails::MailersController
I've tried searching google, the docs, stack overflow, but nothing eludes to this error.
Anyone experience this or have any ideas?
The default preview path is /test/mailers/previews but rspec will override this to be /spec/mailers/previews
You can set the path to be whatever you like with:
config.action_mailer.preview_path = "#{Rails.root}/test/mailers/previews"
This is because you are using UserMailer with NotifierPreview. Change NotifierPreview to UserMailerPreview and it will start working. Check the example implementation https://github.com/RichIsOnRails/ActionMailerPreviewsExample and tutorial.
Cheers!!
If you're using rspec, make sure that the rspec-rails gem is being loaded in the development environment, otherwise Rails will look for the previews under the /test folder, not /spec.
Remove the line(s)
get '/:controller(/:action(/:id))'
get '/:controller(/:action(/:id))(.:format)'
from your routes.rb
As #hellion says in a comment on the previous answer this is the solution to this problem.

Rails3: Find method not working with fixtures in test environment

The Rails Guide says, "Fixtures can also transform themselves into the form of the original class. Thus, you can get at the methods only available to that class," and gives the example,
# using the find method, we grab the "real" david as a User
david = users(:david).find
My fixtures are working ok, but when I try
memb = members(:one).find
I get an error: NoMethodError: undefined method `find'. What am I doing wrong?
(Rails Guide reference: http://edgeguides.rubyonrails.org/testing.html#the-low-down-on-fixtures)
The guide is fixed now, and find is not necessary anymore because now users(:david) is the ActiveRecord object.

Unit test with Authlogic on Rails 3

I would like to write some unit test with a logged user using Authlogic. To start right, I used some code hosted in http://github.com/binarylogic/authlogic_example. But I get an error after rake test, because of "test_helper.rb" and the following class:
class ActionController::TestCase
setup :activate_authlogic
end
Here is my error:
NameError: undefined local variable or
method `activate_authlogic' for
I think this Authlogic example is mapped over Rails 2; maybe it's a little bit different on Rails 3. Is there an other example where I can take example about unit test?
Many thanks.
Do you require 'authlogic/test_case' and include Authlogic::TestCase?
I had a similar issue (using rspec, though) and read through the code at http://github.com/trevmex/authlogic_rails3_example
As of Rails 3.1 and Authlogic 3.0.3, the only thing I've had to add to activate authlogic was
features/support/env.rb
Before do
activate_authlogic
end
I don't realize where to put include Authlogic::TestCase, so I put this after the requires in spec_helper.rb and it worked. There is a better place for it?