I have a class, inside the class there is an instance method like the following:
def validates
#signature.blank?
end
I am doing unit testing on this class, when I try to run test, I get following error:
NoMethodError: undefined method `blank?' for #<String:0x5449061e>
However, when I stop using Rails blank?, and change it to the following it works:
if (!#signature || #signature.empty?)
Does unit testing in Rails allows use of Rails method?
You need to
require 'active_support/core_ext/object'
on top of your test file
Related
I've got this helper which I'm trying to write tests for in Minitest. The helper calls another method depending on the object class I'm passing as an argument, like so:
def label_for(object)
status = object&.status
case object.class.name
when "Subscription"
class_for_subscription_status(status)
when "Payment"
class_for_payment_status(status)
when "Purchase"
class_for_purchase_status(status)
when "Invoice"
class_for_invoice_status(status)
when "Ticket"
class_for_ticket_status(status)
end
Each individual method is already tested somewhere else, so I just need to test that if I pass a class Subscription object to label_for, it will invoke class_for_subscription_status(status) and not something else.
This is the test I've come up with, but I get NoMethodError: undefined method ``class_for_subscription_status' for #<AuxiliariesHelperTest errors.
test "#label_for(object) should invoke the right helper if object is of class Subscription" do
AuxiliariesHelperTest.any_instance.stubs(:label_for).with(subscriptions(:user)).returns(:class_for_subscription_status)
assert_equal class_for_subscription_status(subscriptions(:user).status), label_for(subscriptions(:user))
end
What am I doing wrong?
Could you add the whole classes? Is a little bit hard to guess with just this snippet.
One of the problems I see is that you are stubbing a method from the AuxiliariesHelperTest class, instead of the AuxiliariesHelper class.
Another possible issue is that your helper seems to be a module and not a class, and you should include the helper in your test file. Or your test class should inherit from ActionView::TestCase. Something like this might help:
class AuxiliariesHelperTest < ActionView::TestCase
include AuxiliariesHelper
test "#label_for(object) should invoke the right helper if object is of class Subscription" do
AuxiliariesHelper.any_instance.stubs(:label_for).with(subscriptions(:user)).returns(:class_for_subscription_status)
assert_equal class_for_subscription_status(subscriptions(:user).status), label_for(subscriptions(:user))
end
end
Although in my opinion, you should not stub the method, but expect that the correct method is called:
class AuxiliariesHelperTest < ActionView::TestCase
include AuxiliariesHelper
test "#label_for(object) should invoke the right helper if object is of class Subscription" do
AuxiliariesHelper.any_instance.expects(:label_for).with(subscriptions(:user).status)
label_for(subscriptions(:user))
end
end
In have this model in Rails:
class User < ActiveRecord::Base
def self.create_auth_from_hash(hash)
# stuff
end
end
I want to stub the create_from_auth_hash method so I can isolate the controller behaviour. Apparently the current syntax for this is:
expect_any_instance_of(User).to receive(:create_from_auth_hash).and_return(false)
But even though I get no errors, the model method is still called.
How can I stub model methods? I'm using Rails 4.1 and RSpec 3.0.
Your expectation is set up to stub a method on any instance of User, not the class method you've defined on User.
To do that, you just pass User as the argument to expect, instead of an instance. Like this:
expect(User).to receive(:create_from_auth_hash).and_return(false)
New to RoR, I'm using the Carmen gem, running a rake gives me the error:
Failed: NoMethodError: undefined method `excluded_states=' for Carmen:Module
but the gem includes the attr_accessor method with :excluded_states in the args.
Doesn't the attr_accessor method automagically create the `excluded_states=' setter method?
You can't call attr_accessor within a module, that's something that should only work within a class. What you want instead is the mattr_accessor variant:
module MyModule
mattr_accessor :excluded_states
end
It's also possible it is defined correctly but you're referencing it incorrectly, as in you should be calling that on an instance of something.
I have a test case like this:
describe WorkCardsController do
it "something" do
work_card = instance_double(WorkCard, {:started?=>true} )
#some more code
end
end
When I run RSpec, I get an error:
undefined method 'instance_double' for #<Rspec::Core::ExampleGroup::Nested_1::Nested_8::Nested_3:0x007f0788b98778>
According to http://rubydoc.info/github/rspec/rspec-mocks/RSpec/Mocks/ExampleMethods this method exists. So I tried to access it directly by:
describe WorkCardsController do
it "something" do
work_card = RSpec::Mocks::ExampleMethods::instance_double(WorkCard, {:started?=>true} )
#some more code
end
end
And then I got a very surprising error:
undefined method 'instance_double' for Rspec::Mocks::ExampleMEthods:Module
which is contrary to the documentation I linked above.
What am I missing?
From the documentation you pointed to:
Mix this in to your test context (such as a test framework base class) to use rspec-mocks with your test framework.
Try to include it into your code:
include RSpec::Mocks::ExampleMethods
Your direct approach failed, because calling
RSpec::Mocks::ExampleMethods::instance_double(...)
expects that the method was declared as a class method:
def self.instance_double(...)
but it has been declared as an instance method :
def instance_double(...)
module FooHelper
def foo
haml_tag(:div) do
haml_content("bar")
end
end
end
When I test this I get:
NoMethodError: undefined method `haml_tag'
This code is perfectly valid and works in a development/production environment.
It's something to do with having the haml helpers properly loaded in the test environment.
Thanks!
It looks like the Rails test scaffold isn't including Haml::Helpers in its context. If you're using Test::Unit, you can probably just include it yourself in the test class. You'll also want to run Haml::Helpers#init_haml_helpers in the test setup so that all the Haml stuff is properly initialized.
http://haml-lang.com/docs/yardoc/Haml/Helpers.html#init_haml_helpers-instance_method