Rails for Java Developers phần 8 ppsx

35 309 0
Rails for Java Developers phần 8 ppsx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

TESTING INTERACTIONS WITH MOCK OBJECTS 227 Like Java, Ruby does not shi p 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/te st_helper.rb require 'flexmock' Now for a test. The Rails XT sample application does not have a man- ager layer, so we will intr oduce 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 pref- erences in the session and use a third-party API to filter content. The thir d-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, t o just begin our test case by including Flex- Mock::TestCase: Download code/rails_xt/test/fu nctional/quips_controller_test.rb include FlexMock::TestCase Adding FlexMock::TestCase gives us helper methods for creating mocks, and i t 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/fu nctional/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 err or messages. In th e J ava version, the mock had to have a specific interface so jMock could know what methods the mock should simulate. Since Ruby is dynamically typed, we do not specify any specific module or class for t he mock. On line 3, we set the expectations for the mock. FlexMock takes advan- tage of Ruby’s blocks to set expectations through a recorder object. On line 4, the block parameter m is a recorder. Instead of saying m.should_ expect.filter, we can simply say m.filter; the should_expect is implicit. Flex- Mock’s matching of parameters takes advantage of Ruby’s case equality operator (===). So, the first argument to filter must be an instance of Array. This array will be the result of Quip.find(:all), and we could have chosen t o match it exactly by instantiating the entire collection in th e test. The second argument nil matches the user’s filtering prefer ences, which are initially nil. On lin e 6, we set the controller’s @filter_serviceto our mock filter. By calling instance_variable_set, we avoid the requirement that the controller provide a setter for @filter_service. There is no call to verify at the end of the method; FlexMock mocks verify automatically at the end of the test. Ruby’s blocks and case equality make it easy to define flexible argu- ment matching. Imagine that we wanted to verify that none of the quips passed to the @filter_service has non-nil text. FlexMock would handle this with FlexMock.on: Download code/rails_xt/test/fu nctional/quips_controller_test.rb matcher = FlexMock.on {|args| Array === args && args.all? {|a| a.text}} filter.should_expect do |m| m.filter(matcher,nil).returns([quips(:quip_1)]) end The previous tests demonstrates another advantage of mock objects. Mock objects allow you to test interactions with code that does not exist yet. In testing th e QuipsController, we n ever create a real filter service. At the time of thi s w riting, there is no real filter service. This decoupling lets t eams of developers work on related subsystems without having to wait for completed implementations of every object. The mock objects in this section replace objects not under test and ver- ify that those objects are called in an appropriate fashion. Sometimes you want to replace objects not under test, but you don’t care how they are called. This subset of mock object capability is provided by stub objects. REDUCING DEPENDENCIES WITH STUB OBJECTS 229 7.8 Reducing Dependencies w i th Stub Objects It is all too easy to write fragile tests that depend on other classes. Think about how you might test this simple controller method: Download code/people/app/controllers/people_controller.rb def create @person = Person.new(params[:person]) if @person.save flash[:notice] = 'Person was successfully created.' redirect_to :action => 'list' else render :action => 'new' end end To test both branches of the code, you will need a valid Person and an invalid Person. The problem is that you are supposed to be testin g PersonController, not Person. If you pick valid and invalid arguments for the real Person class, you introduce a dependency on Person. This is a maintenance headache. When you change Person, you will break the PersonTest (OK), but you will also br eak the PersonC o ntrollerTest (aargh). To avoid this problem, we can test a stub version of Person. The stub stub replaces Person with behavior that we define locally, breaking the exter- nal dependency. This probably sounds similar to the mock objects from the previous section, and it is. In fact, we will use the same library for stubs, FlexMock. Here is a stub-based test for creating a Person: Download code/people/test/functional/people_controller_test.rb def test_create_succeeds flexstub(Person).should_receive(:new).and_return { flexmock( 'person' ) do |m| m.should_receive(:save).and_return(true) end } post :create assert_response :redirect assert_redirected_to :action => 'list' end On line 2, flexstub temporarily modifies the behavior of Person. For the remainder of this test, calls to Person.new will invoke this block of code instead. On l i ne 3 we mock an instance of Person, and on line 4 we cause save to always succeed. This test method will test how the controller handles a successful Person create, regardless of how the real Person class works. ADVANCED CONSIDERATIONS 230 Testing the failure case is a little more complex, because the failure case hands the Person instance off to new.rhtml. The template expects a Person to implement various accessors and to return a working errors property. This requires another mock for t he errors collection, plus the should_ignore_missing call to make the mocks more forgiving: Download code/people/test/functional/people_controller_test.rb def test_create_fails flexstub(Person).should_receive(:new).and_return { errs = flexmock( 'errs' ) do |m| m.should_ignore_missing end inst = flexmock( 'person' ) do |m| m.should_ignore_missing m.should_receive(:errors).and_return(errs) m.should_receive(:save).and_return( false) end } post :create assert_response :success assert_template 'new' end Setting up stubs may seem like overkill for small projects, but it can be lifesaver as projects grow. The first time a refactoring sets off a chain of dependencies and breaks 500 tests, you will be wishing for those stubs. 7.9 Advanced Consid erations Now that you have seen the basics of unit testing in Rails, the following are some more advanced issues to think about: Naming Conventions Considered Harmful? The use of naming conventions—such as prefixing all unit tests with “test”—is troubling to some. The more recent versions of JUnit allows the use of Java 5 annotations for marking a test. For example, this is allowed: @Test public void tag() instead of the following: public void testTag() By comparison, Ruby doesn’t have annotations. Since the object model is so flexible, results similar to annotations can usually be achieved with class methods. But nobody in the Ruby community cares. As f ar RESOURCES 231 as we know, nobody has yet felt the need to provide an automated testing solution that avoids the use of naming conventions. One Size Does Not Fit All Not everyone on in the Java world uses JUnit . TestNG 10 is also pop- ular. TestNG addresses a set of limitations in J Unit’s approach to test setup, teardown, and integration wi th automation. Similar limitations in Test::Unit would not/do n ot drive anyone to write a new library. Ruby is flexible enough th at issues with Test::Unit are likely to be handled in an ad hoc way. Behavior-Driven Development One possible competitor to Test::Unit in the Ruby world is RSpec. 11 RSpec is framework f or writing executable specifications of program behavior. In terms of implementation, executable specifications may not be much different from unit tests. But the associated mind-set is different, and th e terminology used in RSpec may lead to better project automation. Java got there first; RSpec is inspired by JBehave. 12 The automated testing f eat ures discussed in this chapter provide a dynamic and active way to verify that your application code works cor- rectly. Dynamic languages like Ruby are particularly well suited to writ- ing automated tests, because it i s easy to create a variety of different test-bed environments. This is fortuitous, since Rails applications need good tests—there is no compiler to catch simple mistakes. Once you have written good tests, th e next obvious step is to auto- mate their invocation on a regular cycle. The next chapter, Chapter 8, Automating the Development Process, on page 233, explains how to use Rake t o automate not just your tests but all the other repetitive tasks associated with software development and deployment. 7.10 Resources A Guide to Testing the Rails http://manuals.rubyonrails.com/read/book/5 The Ruby on Rails manual for writing tests is fairly comprehensive and includes some pieces not covered here such as tests for ActionMailer. 10. http://testng.org 11. http://rspec.rubyforge.org/ 12. http://jbehave.codehaus.org/ RESOURCES 232 Annotation Hammer . . . http://www.infoq.com/articles/Annotation-Hammer Venkat Subramaniam explains Java annotations. In particular, he looks at the decision to use annotation to mark test methods in Java 1.4 and considers the trade-offs between naming conventions and annotation metadata. In Pursuit of Code Quality: Don’t Be Fooled by the Coverage Report. . . . . . http://www-128.ibm.com/developerworks/java/library/j-cq01316 /index.html?ca=drs Andrew Glover analyzes ways the coverage reports can be misused and advises how to use coverage history to guide (but not dictate!) development efforts. Ruby/Rails Unit Testing in Less Than 1 Second. . . . . . http://jayfields.blogspot.com/2006/09/rubyrails-unit-testing-in-less-than-1.html Jay Fields shows how to reduce test dependencies, particularly dependen- cies on the database, and explains how his team uses Stubba and Mocha ( http://rubyforge.org/projects/mocha/) for mock and stub objects. ZenTest . . . . http://rubyforge.org/forum/forum.php?forum_id=8885 ZenTest is a set of tools for doing Extreme Programming (XP) faster with Test::Unit. ZenTest includes tools to generate missing methods, interpret asser- tion diffs, run tests continuously, and automatically test on multiple version of Ruby. Chapter 8 Automating the Dev elopment Process The process of software development begs for a lot of automation. Given the source code and other files that make up a project, you may want to trigger processes to do the following: • Compile the code • Deploy from one environment to another • Vary settings for development, testing, and production • R un automated t est s • Start and stop server processes • Collect profiling data • Manage log files • Handle dependencies on other libraries • Configure databases and data • And on and on and on You can bet that decent-sized projects will have lots of tasks like this. Most of these tasks, in t heir purest, raw for m, can be in dividually trig- gered via some command-line tool (w i th appropriate settings). Remem- bering all the right settin gs, and what order to in voke the tools, is tedious and error-prone. Most programming environments include a basic tool for this kind of automation. In classic Unix development, the basic tool is make. 1 In J ava, the tool is ant. In Ruby, the tool is rake. This chapter explains rake by comparison to ant and then demonstrates some of the ways we use rake to manage Rails applications. 1. We find it wildly amusing that we build this chapter by typing make Rake.pdf instead of rake Rake.pdf. Wonder whether this note will make it through the revi ew process . . RAKE BASICS 234 8.1 Rake Basics In the Java world, rake is called ant. Let’s start with a simple Ant build script that manages the compilation of a Java program: Download code/Rake/simple_ant/build.xml <project name= "simple-ant" default= "compile" > <target name= "clean" > <delete dir= "classes" /> </target> <target name= "prepare" > <mkdir dir= "classes" /> </target> <target name= "compile" depends= "prepare" > <javac srcdir= "src" destdir= "classes" /> </target> </project> Ant build scripts are written in XML. In this example, the top-level project element declares a name, which is the name of the project, and declares the name of the default target to invoke w hen the ant target command-line tool is run. Our default target is compile, so you would expect that this script’s default behavior is to compile Java source code. Here’s the output from ant: $ ant Buildfile: build.xml prepare: [mkdir] Created dir: /Book/code/Rake/simple_ant/classes compile: [javac] Compiling 1 source file to /Book/code/Rake/simple_ant/classes BUILD SUCCESSFUL Total time: 3 seconds Three good things just happened. First, notice that ant does not need to be told to use build.xml; it just assumes that unless told otherwise. This is an example of “convention over configuration.” Second, even though the default target for this script is compile, ant knows to execute the prepare target first. If you refer to the XML configuration file, you can see that compile depends on prepare: <target name= "compile" depends= "prepare" > <javac srcdir= "src" destdir= "classes" /> </target> RAKE BASICS 235 This depends declaration is an example of dependency-based program- ming. You do not have to explicitly call functions in some order. Instead, you just state the dependencies, and the tool figures out th e right order. When you have only a few tasks, this may seem like nothing special; however, when you h ave tens or h undreds of tasks, dependency-based programming can enable cleaner, more readable code. To see the third good thing that happened, you need to run ant again : $ ant Buildfile: build.xml prepare: compile: BUILD SUCCESSFUL Total time: 2 seconds This time, Ant looked at the prepare and compile tasks but did not actu- ally do anything. ant evaluates the dependencies and sees that prepare and compile are already up-to-date. The body of the prepare target calls the mkdir task to create a directory: <target name= "prepare" > <mkdir dir= "classes" /> </target> A task is simply a piece of code to be executed. Many of Ant’s built-in tasks, such as mkdir, are smart enough to do nothing if their work has already been done. This becomes important for time-int ensive tasks such as the javac t ask in the body of compile: <target name= "compile" depends= "prepare" > <javac srcdir= "src" destdir= "classes" /> </target> Now let’s build a simple rake file. Since Ruby programs are not com- piled, we will use a slightly differ ent example. The following rakefile uses Rails’ built-in Code Statistics object to calculate lines of code and a few oth er statistics for some Ruby code: Download code/Rake/simple_rake/rakefile require 'rake/rdoctask' require ' /code_statistics.rb' task :default => :stats task :clean do rm_rf 'stats' end SETTING RAKE OPTIONS: IT’S JUST RUBY 236 task :prepare do mkdir_p 'stats' end task :stats => [:prepare] do require 'code_statistics' File.open( "stats/main.stat" , "w" ) do |f| f << CodeStatistics.new([ 'App Main' , 'src' ]).to_s end end Although this looks quite a bit different from Ant’s build.xml file, they actually have quite a bit in common. Rake, like Ant, defines a set of tasks. Also, tasks can be related by dependencies. The => should be read “depends on.” When you run rake, more similarities appear: $ rake (in /Users/stuart/FR_RAILS4JAVA/Book/code/Rake/simple_rake) mkdir -p stats + + + + + + + + | Name | Lines | LOC | Classes | Methods | M/C | LOC/M | + + + + + + + + | App Main | 1 | 1 | 0 | 0 | 0 | 0 | + + + + + + + + Code LOC: 1 Test LOC: 0 Code to Test Ratio: 1:0.0 Rake automatically knows what file to use—rakefile is the default, just as build.xml is the default for ant. Although ant has a top-level project element specifying the default task, rake has no equivalent. Instead, rake assumes a task named default. To make other tasks run by default, simply make default depend on the other tasks you want to run: Download code/Rake/simple_rake/rakefile task :default => :stats By far the biggest difference is the language syntax. Where Ant uses XML, Rake uses Ruby. All the syntax in a rakefile is “just” Ruby. The task names are symbols, the dependencies are Ruby hashes, and the task bodies are Ruby blocks. If you know Ruby, you know quite a bit of Rake already. 8.2 Setting Rake Options: It’s Just Ruby The ramifications of choosing a programming language (Ruby) instead of a text markup language (XML) are profound, and they become more significant as build files become more complex. To see this, let’s r efactor [...]... before ever appearing in a public release of Rails At the time of this writing, you cannot get it by simply freezing to Edge Rails Instead, you have to check out the most recent version of Rails into the vendor /rails directory of your application: svn co http://dev.rubyonrails.org/svn /rails/ trunk vendor /rails Then, edit your environment.rb to include ActiveResource in the load path: Download code /rails_ xt/config/environment.rb... years, many developers have rediscovered dynamic languages such as JavaScript, PHP, Python, and Ruby as an alternative to the perceived complexity of Java and C# development This search for simplicity has happened with data format as well, with the rise of YAML as an alternative to XML 9.3 YAML and XML Compared XML is the dominant data format for web service development There are some good reasons for this... you are ready to use the entire Rails stack for website development The chapters that follow expand from this base to web applications: web-based programs that deliver data and services beyond browser content 8. 6 Resources Apache Maven Simplifies the Java Build Process http://www.devx.com /java/ Article/17204 Well-written introduction to Maven Maven is a build tool for Java that relies far more on convention... authors_path %> It looks like Rails is asking the form to use the PUT verb, but the generated code tells a different story: Download code /rails_ xt/samples/rest/simulated_put.html for brevity > Rails stores the intended action in a hidden _method field If the HTTP method is POST, Rails checks _method and... load path 241 U SING R AKE IN R AILS A PPLICATIONS rake rails: freeze:gems Copies current gems into vendor /rails rake rails: freeze:edge REVISION=nnn Copies svn revision nnn into vendor /rails rake rails: freeze:edge TAG=rel_1-1-0 Copies svn tag rel_1-1-0 into vendor /rails On the other hand, you might want to take less control of which version of Rails you get (This sounds unlikely but might be true during... of Rails. ) These tasks work by associating your Rails application with a copy of Rails that is not directly managed by your project rake rails: freeze:edge Puts the svn edge (most recent revision) into vendor /rails rake rails: unfreeze Undoes any freeze; back to depending on gems If you are using Subversion as your version control system, you can use its svn:externals facility to link the vendor /rails. .. Markup Language (SGML), a metalanguage for defining document markup XML has become so popular that it is used for all things data In the Java world, XML has become the standard mechanism for application configuration files Many Java programmers have come to believe that XML is overkill for this purpose To see why, imagine a hypothetical configuration file that stores user information: stu... implements Rails RESTful routing We’ll explain this line in detail after a few examples The scaffold generates a set of files whose names and locations are similar to the original Rails scaffold The code in these files looks quite different Here is the index method: Download code /rails_ xt/app/controllers/authors_controller.rb def index @authors = Author.find(:all) respond_to do |format| format.html format.xml... version of Rails your application will use By default, your Rails application will use the latest gems on your machine If you control when and how gems are installed on a machine and have only one Rails application, this may be fine You can be more conservative in several ways and request a specific version of Rails The tasks in the list that follows work by copying Rails into the vendor /rails directory... vendor /rails directory to the official Rails repository When you svn up the most recent changes to your own project, you will also get the latest, greatest, not-yet-released version of Rails This is not recommended for production servers! Rails also copies some files into your project that may need to be updated to take advantage of newer versions of Rails The rake rails: update task, and its subtasks, . and Mocha ( http://rubyforge.org/projects/mocha/) for mock and stub objects. ZenTest . . . . http://rubyforge.org/forum/forum.php?forum_id =88 85 ZenTest is a set of tools for doing Extreme Programming. Testing the Rails http://manuals.rubyonrails.com/read/book/5 The Ruby on Rails manual for writing tests is fairly comprehensive and includes some pieces not covered here such as tests for ActionMailer. 10 RAILS APPLICATIONS 242 rake rails: freeze:gems Copies current gems into vendor /rails rake rails: freeze:edge REVISION=nnn Copies svn revision nnn into vendor /rails rake rails: freeze:edge TAG=rel_1-1-0 Copies

Ngày đăng: 06/08/2014, 09:20

Tài liệu cùng người dùng

Tài liệu liên quan