Reputation: 1267
I am trying to use Minitest in a fresh Rails 4 install. My understanding is that if I have a class that doesn't inherit from ActiveRecord then I should be able to use Minitest itself, without Rails integration:
#test/models/blog.rb
require "minitest/autorun"
class Blog < Minitest::Unit::TestCase
def setup
@b = Blog.new
end
def test_entries
assert_empty "message", @b.entries
end
#app/models/blog.rb
class Blog
attr_reader :entries
def initialize
@entries = []
end
I run the test with ruby test/models/blog.rb
.
My problem comes with the setup method. If I don't include an entry for my blog, the tests fails with the message that there are the wrong number of arguments in setup. If I include an entry in my setup message @b = Blog.new entries: "Twilight"
, my test fails in the test_entries
method because entries is an undefined method.
Upvotes: 1
Views: 785
Reputation: 8984
You have a couple problems. First, you are not requiring "test_helper", which means that rails isn't getting loaded when you run this test, which means that the mechanism rails uses to resolve missing constants isn't loaded. You will either need to require the helper or require the blog file directly. Second, you are overwriting the constant you want to test with the test, which is why you are getting confusing messages. Name the test class BlogTest
instead to avoid this.
This is what I think you are trying to do:
require "minitest/autorun"
require "models/blog" # Assuming "app" is in your load path when running the test
#require "test_helper" # Or require this instead if you need to use DB
class BlogTest < Minitest::Unit::TestCase
def setup
@b = Blog.new
end
def test_entries
assert_empty @b.entries, "Blog entries should be empty"
end
end
Upvotes: 3