Skip to content

Commit

Permalink
Merge cf6002b into fb09919
Browse files Browse the repository at this point in the history
  • Loading branch information
JunichiIto committed Oct 4, 2014
2 parents fb09919 + cf6002b commit 3b9bfe5
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 85 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea
.rvmrc
.ruby-version

## MAC OS
.DS_Store
Expand Down
40 changes: 20 additions & 20 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ A basic generator spec might look something like this:
describe :custom_controller do
context "with no arguments or options" do
it "should generate a help message" do
subject.should output("A Help Message")
expect(subject).to output("A Help Message")
end
end

with_args :users do
it "should generate a UsersController" do
subject.should generate("app/controllers/users_controller.rb") { |content|
content.should =~ /class UserController/
expect(subject).to generate("app/controllers/users_controller.rb") { |content|
expect(content).to match /class UserController/
}
end
end
Expand All @@ -54,29 +54,29 @@ You may need to specify a base class, particularly if you are testing a Rails en
This is the preferred way to test files that were generated, because this matcher checks your generator's *behavior*. The test won't care _how_ a file is generated, as long as it _is_ generated. It's as simple as passing the name of the file you expected to be generated:

it "should generate a readme file" do
subject.should generate("README")
expect(subject).to generate("README")
end

You can also check the generated file's content by simply passing a block. The _content_ argument in the block is
a simple String containing the content of the file:

it "should generate a model called 'user'" do
subject.should generate("app/models/user.rb") { |content|
content.should =~ /class User < ActiveRecord\:\:Base/
expect(subject).to generate("app/models/user.rb") { |content|
expect(content).to match /class User < ActiveRecord\:\:Base/
}
end

You can also very simply ensure that the generator runs without error, without any further validation, by omitting all arguments:

it "should generate successfully" do
subject.should generate
expect(subject).to generate
end

Finally, you could pass a block but no other arguments to +generate+ in order to check the generator's results the old-fashioned way:

it "should generate a model called 'user'" do
subject.should generate {
File.read("app/models/user.rb").should =~ /class User < ActiveRecord\:\:Base/
expect(subject).to generate {
expect(File.read("app/models/user.rb")).to match /class User < ActiveRecord\:\:Base/
}
end

Expand All @@ -89,31 +89,31 @@ However, sometimes you need to verify that some action occurs which can't be val
All 3 of the following examples perform exactly the same test. Use whichever seems the most expressive to you. (I prefer the first one.)

it "should add a gem source" do
subject.should add_source("http://gems.github.com")
expect(subject).to add_source("http://gems.github.com")
end

# -or-
it "should add a gem source" do
subject.should call_action(:add_source, "http://gems.github.com")
expect(subject).to call_action(:add_source, "http://gems.github.com")
end

# -or-
it "should add a gem source" do
subject.should generate(:add_source, "http://gems.github.com")
expect(subject).to generate(:add_source, "http://gems.github.com")
end

You can stop passing arguments at any time. This has the effect of widening the range of acceptable parameters. For
instance, the following example does the same thing but will accept _any_ source URL, as long as the _add_source_
action is called:

it "should add a gem source" do
subject.should generate(:add_source)
expect(subject).to generate(:add_source)
end

Similarly, you can get away with specifying only the some of a sequence of arguments; the omitted arguments will accept any value, while the specified ones will be tested. Another example:

it "should inject into file" do
subject.should inject_into_file("config/environment.rb", "config.gem :thor")
expect(subject).to inject_into_file("config/environment.rb", "config.gem :thor")
end

# if the generator includes the following action, the test will
Expand All @@ -133,12 +133,12 @@ If you need to test the generator's feedback rather than the generator's results

# Example 1: String
it "should generate a help message" do
subject.should output("A Help Message")
expect(subject).to output("A Help Message")
end

# Example 2: Regular Expression
it "should generate a help message" do
subject.should output(/A [hH]elp Message/)
expect(subject).to output(/A [hH]elp Message/)
end


Expand Down Expand Up @@ -217,10 +217,10 @@ Here's an example that verifies that a file is created by the generator, but tha

describe :controller do
with_args "welcome" do
it { should generate("app/controllers/welcome_controller.rb") }
it { is_expected.to generate("app/controllers/welcome_controller.rb") }

with_options :behavior => :revoke do
it { should delete("app/controllers/welcome_controller.rb") }
it { is_expected.to delete("app/controllers/welcome_controller.rb") }
end
end
end
Expand All @@ -247,8 +247,8 @@ You can even nest such structures within various contexts:
within_source_root { touch "config/routes.rb" }

it "should insert the new route" do
subject.should generate {
File.read("config/routes.rb").should_not be_blank
expect(subject).to generate {
expect(File.read("config/routes.rb")).to_not be_blank
}
end
end
Expand Down
2 changes: 1 addition & 1 deletion genspec.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |s|
s.test_files = Dir["spec/**/*"]

s.add_dependency 'thor'
s.add_dependency 'rspec', '~> 2'
s.add_dependency 'rspec', '~> 3'
s.add_dependency 'sc-core-ext', "~> 1.2.1"

s.add_development_dependency 'coveralls'
Expand Down
2 changes: 1 addition & 1 deletion lib/genspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def self.root=(root); @root = root; end
end

RSpec.configure do |config|
config.include GenSpec::GeneratorExampleGroup, :example_group => { :file_path => /spec[\/]generators/ }
config.include GenSpec::GeneratorExampleGroup, :file_path => /spec[\/]generators/

# Kick off the action wrappers.
#
Expand Down
2 changes: 1 addition & 1 deletion lib/genspec/generator_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def generator(name = nil)
if genspec_subclass?
superclass.generator
else
describes || description
described_class || description
end
end

Expand Down
18 changes: 9 additions & 9 deletions lib/genspec/matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ module Matchers
# :migration_template, :route_resources
#
# Examples:
# subject.should generate(:file, ...)
# subject.should generate("vendor/plugins/will_paginate/init.rb")
# expect(subject).to generate(:file, ...)
# expect(subject).to generate("vendor/plugins/will_paginate/init.rb")
#
def generate(kind = nil, *args, &block)
if kind.kind_of?(Symbol)
# subject.should generate(:file, ...)
# expect(subject).to generate(:file, ...)
call_action(kind, *args, &block)
else
# subject.should generate("vendor/plugins/will_paginate/init.rb")
# expect(subject).to generate("vendor/plugins/will_paginate/init.rb")
GenSpec::Matchers::ResultMatcher.new(kind, &block)
end
end
Expand All @@ -27,19 +27,19 @@ def generate(kind = nil, *args, &block)
# completes its run.
#
# Example:
# subject.should delete("path/to/file")
# expect(subject).to delete("path/to/file")
#
def delete(filename)
within_source_root do
FileUtils.mkdir_p File.dirname(filename)
FileUtils.touch filename
end

generate { File.should_not exist(filename) }
generate { expect(File).to_not exist(filename) }
end

# ex:
# subject.should call_action(:create_file, ...)
# expect(subject).to call_action(:create_file, ...)
#
def call_action(kind, *args, &block)
GenSpec::Matchers::GenerationMethodMatcher.for_method(kind, *args, &block)
Expand All @@ -56,9 +56,9 @@ def add_shorthand_methods(base)
instance_methods = base.instance_methods.collect { |m| m.to_s }

# ex:
# subject.should create_file(...)
# expect(subject).to create_file(...)
# equivalent to:
# subject.should call_action(:create_file, ...)
# expect(subject).to call_action(:create_file, ...)

GenSpec::Matchers::GenerationMethodMatcher.generation_methods.each do |method_name|
# don't overwrite existing methods. since the user expects this to fire FIRST,
Expand Down
8 changes: 4 additions & 4 deletions lib/genspec/matchers/generation_method_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def failure_message
"expected to generate a call to #{method_name.inspect}#{with_args} but #{what}"
end

def negative_failure_message
def failure_message_when_negated
"expected not to generate a call to #{method_name.inspect}#{with_args} but it happened anyway"
end

Expand Down Expand Up @@ -109,7 +109,7 @@ class << self
# GENERATION_CLASSES. This is the list of methods that will be converted
# into matchers, which can be used like so:
#
# subject.should create_file(. . .)
# expect(subject).to create_file(. . .)
#
# See also GENERATION_CLASSES
#
Expand All @@ -127,10 +127,10 @@ def generation_methods
# called from GenSpec::Matchers#call_action
#
# example:
# subject.should call_action(:create_file, ...)
# expect(subject).to call_action(:create_file, ...)
#
# equivalent to:
# subject.should GenSpec::Matchers::GenerationMethodMatcher.for_method(:create_file, ...)
# expect(subject).to GenSpec::Matchers::GenerationMethodMatcher.for_method(:create_file, ...)
#
def for_method(which, *args, &block)
if generation_methods.include?(which.to_s)
Expand Down
2 changes: 1 addition & 1 deletion lib/genspec/matchers/output_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def failure_message
"expected to match #{@regexp.inspect}, but did not"
end

def negative_failure_message
def failure_message_when_negated
output + "\n" \
"expected not to match #{@regexp.inspect}, but did"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/genspec/matchers/result_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def failure_message
"Expected to generate #{filename}"
end

def negative_failure_message
def failure_message_when_negated
"Expected to not generate #{filename}"
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/generators/migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
describe :my_migration do
it "should run migration template" do
# bug, raising NameError: undefined local variable or method `interceptor'
proc { subject.should generate(:migration_template, "1", "2") }.should_not raise_error
expect { expect(subject).to generate(:migration_template, "1", "2") }.not_to raise_error
end
end
end
18 changes: 10 additions & 8 deletions spec/generators/question_spec.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
require 'spec_helper'
# FIXME Without this, test fails when ENV['USE_RAILS'] = '1'
require_relative '../../spec/support/generators/question/question_generator'

describe :question do
describe Question do
context "without input" do
it "should raise an error" do
proc { subject.should output("Are you a GOD?") }.should raise_error
expect { expect(subject).to output("Are you a GOD?") }.to raise_error
end
end

with_input "yes" do
it "should act upon something" do
subject.should act_upon("something")
subject.should output(/Acted upon something/)
expect(subject).to act_upon("something")
expect(subject).to output(/Acted upon something/)
end

it "should not raise an error" do
proc { subject.should output("Good.") }.should_not raise_error
expect { expect(subject).to output("Good.") }.not_to raise_error
end
end

with_input "no" do
it "should act upon something" do
subject.should act_upon("something")
subject.should output(/Acted upon something/)
expect(subject).to act_upon("something")
expect(subject).to output(/Acted upon something/)
end

it "should not raise an error" do
proc { subject.should output("You're new around here, aren't you?") }.should_not raise_error
expect { expect(subject).to output("You're new around here, aren't you?") }.not_to raise_error
end
end
end
Loading

0 comments on commit 3b9bfe5

Please sign in to comment.