minitest

What's the purpose of assigns method in Rails tests (MiniTest)?

倾然丶 夕夏残阳落幕 提交于 2019-12-04 15:36:03
问题 Used in automatically generated tests: test "should create item" do login_user assert_difference('Item.count') do post :create, item: { creator: @item.creator, title: @item.title, user_id: @item.user_id, text: 'Hello, world!' } end assert_redirected_to(assigns(:item)) end Rails documentation doesn't have any description. What's the purpose of this method and how to use it? 回答1: It means if a controller defined an instance variable @item="something" . You can fetch an instance variable in your

How to organize minitest/unit tests?

独自空忆成欢 提交于 2019-12-04 14:57:24
问题 After using RSpec for several projects, I'm giving minitest/unit a go. I'm liking it so far, but I miss using describe/context blocks to group my tests/specs in a logical way. I know minitest/spec provides this functionality, but I like that minitest/unit feels a bit closer to barebones Ruby. Are there any gems that provide describe/context support for minitest/unit? Or, should I just live with my long, unorganized test files in minitest/unit? 回答1: I know several folks coming from RSpec to

How can I mock with a block in minitest?

狂风中的少年 提交于 2019-12-04 09:15:44
Hopefully a simple question for MiniTest folks.. I have a section of code which I'll condense into an example here: class Foo def initialize(name) @sqs = Aws::SQS::Client.new @id = @sqs.create_queue( queue_name: name ).fetch(:queue_url) @poller = Aws::SQS::QueuePoller.new(@id) end def pick_first @poller.poll(idle_timeout: 60) do |message| process_msg(message) if some_condition(message) end end How can I mock/stub/something-else so that I can feed a message through to be tested by the some_condition() and possibly processed with process_msg() ? I.e. I want to test the @poller.poll(idle_timeout:

How do I mock a Class with Ruby?

放肆的年华 提交于 2019-12-04 07:21:13
I'm using minitest/mock and would like to mock a class. I'm not trying to test the model class itself, but rather trying to test that a service (SomeService) interacts with the model (SomeModel). I came up with this (Hack::ClassDelegate), but I'm not convinced it's a good idea: require 'minitest/autorun' require 'minitest/mock' module Hack class ClassDelegate def self.set_delegate(delegate); @@delegate = delegate; end def self.method_missing(sym, *args, &block) @@delegate.method_missing(sym, *args, &block) end end end class TestClassDelegation < MiniTest::Unit::TestCase class SomeModel < Hack:

can't get test unit startup to work in ruby 1.9.2

大兔子大兔子 提交于 2019-12-04 05:18:04
I am using Ruby 1.9.2 (ruby -v yields :ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-linux]), and I am trying to get this to work: require 'test/unit' class TestStartup < Test::Unit::TestCase def self.startup puts "startup" end def test1 puts "in test1" end end when I run it, I get Loaded suite test_startup Started in test1 . Finished in 0.000395 seconds. 1 tests, 0 assertions, 0 failures, 0 errors, 0 skips I had a hard time finding documentation on this feature, other than scattered posts here on SO and the like. And yes, I want to use this feature and not setup. TIA Ruby 1.9.x bundles

URI::InvalidURIError: bad URI(is not URI?) testing Rails controllers

南楼画角 提交于 2019-12-04 02:53:07
问题 I get URI::InvalidURIError testing Rails Home controller: require 'test_helper' class HomeControllerTest < ActionDispatch::IntegrationTest test "should get index" do get :index assert_response :success end end get the following error: E Error: HomeControllerTest#test_should_get_index: URI::InvalidURIError: bad URI(is not URI?): http://www.example.com:80index test/controllers/home_controller_test.rb:7:in `block in <class:HomeControllerTest>' The stack is the following: Rails 5.0.0.beta3

Idiomatically mock OpenURI.open_uri with Minitest

元气小坏坏 提交于 2019-12-04 02:03:54
问题 I have code that invokes OpenURI.open_uri and I want to confirm the URI being used in the call (so a stub isn't going to work for me), but also intercept the call. I'm hoping not to have to abstract away the call to OpenURI.open_uri just for test purposes. What I've come up with seems verbose and overly complicated. under_test.rb require 'open-uri' class UnderTest def get_request(uri) open(uri).read end end test_under_test.rb require 'minitest/autorun' require './lib/under_test' class

Rails and MiniTest: add additional folder

瘦欲@ 提交于 2019-12-04 00:27:28
I use Ruby 2 and Rails 4. I have a folder test/lib , where a few tests are located. But running rake test does not use them. Only the other tests (models, controllers, ...) are running. Where do I have to add the lib folder? I already tried MiniTest::Rails::Testing.default_tasks << 'lib' , but I get NameError Exception: uninitialized constant MiniTest::Rails . I did not add the minitest gem to my Gemfile, because Ruby 2 uses it by default. To use MiniTest::Rails::Testing.default_tasks << 'lib' you need to add the minitest-rails gem to your Gemfile. It is separate from Minitest and adds enables

Is it still advisable to test routes in Rails 4 with Minitest?

依然范特西╮ 提交于 2019-12-03 14:10:49
In Rails 3, when writing functional tests in MiniTest, I got in the habit of testing routes separately from testing my controller actions. I got the idea from the Rails Guide on Testing - Section 9: Testing Routes . However, after upgrading my application to Rails 4 I noticed that the controller action tests themselves have started balking about unknown routes if I don't supply the get|patch|post|delete method with a proper set of params. For example, given routes: # config/routes.rb namespace "api" do namespace "v2", defaults: { format: :json } do resources :users do resources :posts do

Has anyone used Minitest::Spec withing a Rails functional test?

不羁的心 提交于 2019-12-03 14:07:41
The spec library in Minitest is great. I've been able to use it within Rails unit tests no problem. However, Rails functional test inherit from ActionController::TestCase which provides instance variables like @controller in it's setup. Has anyone been using the Minitest::Spec lib for their Rails functional tests? If not, I'm considering creating a small library to do just that. I'm not too keen on rspec, and shoulda is starting to shift it's focus to rspec. It would be nice to have something lightweight and built on tools already provided. james2m Thoughtbot have now split shoulda into