Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Cú pháp dạng thủ tục (procedural syntax) vẫn còn được hỗ trợ, có vẻ như là ngoài tầm vực của mọi đối tượng, nhưng thực sự là thuộc một thể hiện của class Object tên là 'main'. Vì class này là cha của mọi class khác, nó trở trên ẩn đối với mọi lớp và đối tượng. | Testing Interactions with Mock Objects 227 Like Java Ruby does not ship with a mock object library. We will use FlexMock.9 You can install FlexMock via RubyGems gem install flexmock To make FlexMock available to all tests in a Rails application require it in test_helper.rb Download code rails_xt test test_helper.rb require flexmock Now for a test. The Rails XT sample application does not have a manager layer so we will introduce a new feature in the controller layer. Instead of simply accessing all quips users should be allowed to filter quips based on their preferences. our application will store user preferences in the session and use a third-party API to filter content. The third-party API will be implemented through a @filter_service instance on the controller. It is possible to call the FlexMock API via freestanding classes. It is much simpler however to just begin our test case by including Flex-Mock TestCase Download code rails_xt test functional quips_controller_test.rb include FlexMock TestCase Adding FlexMock TestCase gives us helper methods for creating mocks and it automatically validates the mocks during teardown. The QuipsController should provide a new method list_with_user_filter. This method should return all quips minus any that are rejected by the FilterService. Here is the test Download code rails_xt test functional quips_controller_test.rb def test_list_with_user_filter filter flexmock filter filter.should_expect do m m.filter Array nil .returns quips quip_1 end @controller.instance_variable_set @filter_service filter get list_with_user_filter assert_equal quips quip_1 assigns quips assert_response success assert_template list_with_user_filter end 9. http onestepback.org software flexmock Testing Interactions with Mock Objects 228 On line 2 the flexmock method creates a mock object. The argument is a name that will be used in error messages. In the Java version the mock had to have a specific interface so jMock could know what methods the mock