1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Removed Mocha from ActionView part 1

This commit is contained in:
Ronak Jangir 2015-08-22 09:33:53 +05:30
parent 9503e65b97
commit 0a5b08a1e7
8 changed files with 103 additions and 79 deletions

View file

@ -16,6 +16,7 @@ silence_warnings do
end
require 'active_support/testing/autorun'
require 'active_support/testing/method_call_assertions'
require 'action_controller'
require 'action_view'
require 'action_view/testing/resolvers'
@ -281,3 +282,6 @@ def jruby_skip(message = '')
end
require 'mocha/setup' # FIXME: stop using mocha
class ActiveSupport::TestCase
include ActiveSupport::Testing::MethodCallAssertions
end

View file

@ -588,11 +588,13 @@ class AssetTagHelperTest < ActionView::TestCase
end
end
@controller.request.stubs(:ssl?).returns(false)
assert_equal "http://assets15.example.com/images/xml.png", image_path("xml.png")
@controller.request.stub(:ssl?, false) do
assert_equal "http://assets15.example.com/images/xml.png", image_path("xml.png")
end
@controller.request.stubs(:ssl?).returns(true)
assert_equal "http://localhost/images/xml.png", image_path("xml.png")
@controller.request.stub(:ssl?, true) do
assert_equal "http://localhost/images/xml.png", image_path("xml.png")
end
end
end

View file

@ -2003,21 +2003,22 @@ class FormHelperTest < ActionView::TestCase
def test_form_for_with_remote_without_html
@post.persisted = false
@post.stubs(:to_key).returns(nil)
form_for(@post, remote: true) do |f|
concat f.text_field(:title)
concat f.text_area(:body)
concat f.check_box(:secret)
end
@post.stub(:to_key, nil) do
form_for(@post, remote: true) do |f|
concat f.text_field(:title)
concat f.text_area(:body)
concat f.check_box(:secret)
end
expected = whole_form("/posts", "new_post", "new_post", remote: true) do
"<input name='post[title]' type='text' id='post_title' value='Hello World' />" +
"<textarea name='post[body]' id='post_body'>\nBack to the hill and over it again!</textarea>" +
"<input name='post[secret]' type='hidden' value='0' />" +
"<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />"
end
expected = whole_form("/posts", "new_post", "new_post", remote: true) do
"<input name='post[title]' type='text' id='post_title' value='Hello World' />" +
"<textarea name='post[body]' id='post_body'>\nBack to the hill and over it again!</textarea>" +
"<input name='post[secret]' type='hidden' value='0' />" +
"<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />"
end
assert_dom_equal expected, output_buffer
assert_dom_equal expected, output_buffer
end
end
def test_form_for_without_object
@ -2220,16 +2221,17 @@ class FormHelperTest < ActionView::TestCase
def test_submit_with_object_as_new_record_and_locale_strings
with_locale :submit do
@post.persisted = false
@post.stubs(:to_key).returns(nil)
form_for(@post) do |f|
concat f.submit
end
@post.stub(:to_key, nil) do
form_for(@post) do |f|
concat f.submit
end
expected = whole_form('/posts', 'new_post', 'new_post') do
"<input name='commit' data-disable-with='Create Post' type='submit' value='Create Post' />"
end
expected = whole_form('/posts', 'new_post', 'new_post') do
"<input name='commit' data-disable-with='Create Post' type='submit' value='Create Post' />"
end
assert_dom_equal expected, output_buffer
assert_dom_equal expected, output_buffer
end
end
end
@ -2807,11 +2809,12 @@ class FormHelperTest < ActionView::TestCase
def test_nested_fields_label_translation_with_more_than_10_records
@post.comments = Array.new(11) { |id| Comment.new(id + 1) }
I18n.expects(:t).with('post.comments.body', default: [:"comment.body", ''], scope: "helpers.label").times(11).returns "Write body here"
form_for(@post) do |f|
f.fields_for(:comments) do |cf|
concat cf.label(:body)
params = 11.times.map { ['post.comments.body', default: [:"comment.body", ''], scope: "helpers.label"] }
assert_called_with(I18n, :t, params, returns: "Write body here") do
form_for(@post) do |f|
f.fields_for(:comments) do |cf|
concat cf.label(:body)
end
end
end
end

View file

@ -14,8 +14,9 @@ class FormOptionsHelperI18nTests < ActionView::TestCase
end
def test_select_with_prompt_true_translates_prompt_message
I18n.expects(:translate).with('helpers.select.prompt', { :default => 'Please select' })
select('post', 'category', [], :prompt => true)
assert_called_with(I18n, :translate, ['helpers.select.prompt', { :default => 'Please select' }]) do
select('post', 'category', [], :prompt => true)
end
end
def test_select_with_translated_prompt

View file

@ -108,10 +108,11 @@ class LookupContextTest < ActiveSupport::TestCase
end
test "found templates respects given formats if one cannot be found from template or handler" do
ActionView::Template::Handlers::Builder.expects(:default_format).returns(nil)
@lookup_context.formats = [:text]
template = @lookup_context.find("hello", %w(test))
assert_equal [:text], template.formats
assert_called(ActionView::Template::Handlers::Builder, :default_format, returns: nil) do
@lookup_context.formats = [:text]
template = @lookup_context.find("hello", %w(test))
assert_equal [:text], template.formats
end
end
test "adds fallbacks to view paths when required" do
@ -210,45 +211,50 @@ end
class LookupContextWithFalseCaching < ActiveSupport::TestCase
def setup
@resolver = ActionView::FixtureResolver.new("test/_foo.erb" => ["Foo", Time.utc(2000)])
ActionView::Resolver.stubs(:caching?).returns(false)
@lookup_context = ActionView::LookupContext.new(@resolver, {})
end
test "templates are always found in the resolver but timestamp is checked before being compiled" do
template = @lookup_context.find("foo", %w(test), true)
assert_equal "Foo", template.source
ActionView::Resolver.stub(:caching?, false) do
template = @lookup_context.find("foo", %w(test), true)
assert_equal "Foo", template.source
# Now we are going to change the template, but it won't change the returned template
# since the timestamp is the same.
@resolver.hash["test/_foo.erb"][0] = "Bar"
template = @lookup_context.find("foo", %w(test), true)
assert_equal "Foo", template.source
# Now we are going to change the template, but it won't change the returned template
# since the timestamp is the same.
@resolver.hash["test/_foo.erb"][0] = "Bar"
template = @lookup_context.find("foo", %w(test), true)
assert_equal "Foo", template.source
# Now update the timestamp.
@resolver.hash["test/_foo.erb"][1] = Time.now.utc
template = @lookup_context.find("foo", %w(test), true)
assert_equal "Bar", template.source
# Now update the timestamp.
@resolver.hash["test/_foo.erb"][1] = Time.now.utc
template = @lookup_context.find("foo", %w(test), true)
assert_equal "Bar", template.source
end
end
test "if no template was found in the second lookup, with no cache, raise error" do
template = @lookup_context.find("foo", %w(test), true)
assert_equal "Foo", template.source
ActionView::Resolver.stub(:caching?, false) do
template = @lookup_context.find("foo", %w(test), true)
assert_equal "Foo", template.source
@resolver.hash.clear
assert_raise ActionView::MissingTemplate do
@lookup_context.find("foo", %w(test), true)
@resolver.hash.clear
assert_raise ActionView::MissingTemplate do
@lookup_context.find("foo", %w(test), true)
end
end
end
test "if no template was cached in the first lookup, retrieval should work in the second call" do
@resolver.hash.clear
assert_raise ActionView::MissingTemplate do
@lookup_context.find("foo", %w(test), true)
end
ActionView::Resolver.stub(:caching?, false) do
@resolver.hash.clear
assert_raise ActionView::MissingTemplate do
@lookup_context.find("foo", %w(test), true)
end
@resolver.hash["test/_foo.erb"] = ["Foo", Time.utc(2000)]
template = @lookup_context.find("foo", %w(test), true)
assert_equal "Foo", template.source
@resolver.hash["test/_foo.erb"] = ["Foo", Time.utc(2000)]
template = @lookup_context.find("foo", %w(test), true)
assert_equal "Foo", template.source
end
end
end

View file

@ -118,15 +118,17 @@ class TestERBTemplate < ActiveSupport::TestCase
def test_refresh_with_templates
@template = new_template("Hello", :virtual_path => "test/foo/bar")
@template.locals = [:key]
@context.lookup_context.expects(:find_template).with("bar", %w(test/foo), false, [:key]).returns("template")
assert_equal "template", @template.refresh(@context)
assert_called_with(@context.lookup_context, :find_template,["bar", %w(test/foo), false, [:key]], returns: "template") do
assert_equal "template", @template.refresh(@context)
end
end
def test_refresh_with_partials
@template = new_template("Hello", :virtual_path => "test/_foo")
@template.locals = [:key]
@context.lookup_context.expects(:find_template).with("foo", %w(test), true, [:key]).returns("partial")
assert_equal "partial", @template.refresh(@context)
assert_called_with(@context.lookup_context, :find_template,[ "foo", %w(test), true, [:key]], returns: "partial") do
assert_equal "partial", @template.refresh(@context)
end
end
def test_refresh_raises_an_error_without_virtual_path

View file

@ -20,6 +20,7 @@ module ActionView
class TestCase
helper ASharedTestHelper
DeveloperStruct = Struct.new(:name)
module SharedTests
def self.included(test_case)
@ -50,7 +51,7 @@ module ActionView
end
test "works without testing a helper module" do
assert_equal 'Eloy', render('developers/developer', :developer => stub(:name => 'Eloy'))
assert_equal 'Eloy', render('developers/developer', :developer => DeveloperStruct.new('Eloy'))
end
test "can render a layout with block" do
@ -69,13 +70,15 @@ module ActionView
end
test "delegates notice to request.flash[:notice]" do
view.request.flash.expects(:[]).with(:notice)
view.notice
assert_called_with(view.request.flash, :[], [:notice]) do
view.notice
end
end
test "delegates alert to request.flash[:alert]" do
view.request.flash.expects(:[]).with(:alert)
view.alert
assert_called_with(view.request.flash, :[], [:alert]) do
view.alert
end
end
test "uses controller lookup context" do
@ -119,7 +122,7 @@ module ActionView
test "helper class that is being tested is always included in view instance" do
@controller.controller_path = 'test'
@customers = [stub(:name => 'Eloy'), stub(:name => 'Manfred')]
@customers = [DeveloperStruct.new('Eloy'), DeveloperStruct.new('Manfred')]
assert_match(/Hello: EloyHello: Manfred/, render(:partial => 'test/from_helper'))
end
end
@ -255,15 +258,15 @@ module ActionView
end
test "is able to render partials with local variables" do
assert_equal 'Eloy', render('developers/developer', :developer => stub(:name => 'Eloy'))
assert_equal 'Eloy', render('developers/developer', :developer => DeveloperStruct.new('Eloy'))
assert_equal 'Eloy', render(:partial => 'developers/developer',
:locals => { :developer => stub(:name => 'Eloy') })
:locals => { :developer => DeveloperStruct.new('Eloy') })
end
test "is able to render partials from templates and also use instance variables" do
@controller.controller_path = "test"
@customers = [stub(:name => 'Eloy'), stub(:name => 'Manfred')]
@customers = [DeveloperStruct.new('Eloy'), DeveloperStruct.new('Manfred')]
assert_match(/Hello: EloyHello: Manfred/, render(:file => 'test/list'))
end
@ -272,7 +275,7 @@ module ActionView
view
@customers = [stub(:name => 'Eloy'), stub(:name => 'Manfred')]
@customers = [DeveloperStruct.new('Eloy'), DeveloperStruct.new('Manfred')]
assert_match(/Hello: EloyHello: Manfred/, render(:file => 'test/list'))
end

View file

@ -42,14 +42,16 @@ class TranslationHelperTest < ActiveSupport::TestCase
end
def test_delegates_setting_to_i18n
I18n.expects(:translate).with(:foo, :locale => 'en', :raise => true).returns("")
translate :foo, :locale => 'en'
assert_called_with(I18n, :translate, [:foo, :locale => 'en', :raise => true], returns: "") do
translate :foo, :locale => 'en'
end
end
def test_delegates_localize_to_i18n
@time = Time.utc(2008, 7, 8, 12, 18, 38)
I18n.expects(:localize).with(@time)
localize @time
assert_called_with(I18n, :localize, [@time]) do
localize @time
end
end
def test_returns_missing_translation_message_wrapped_into_span
@ -125,8 +127,9 @@ class TranslationHelperTest < ActiveSupport::TestCase
end
def test_translate_escapes_interpolations_in_translations_with_a_html_suffix
word_struct = Struct.new(:to_s)
assert_equal '<a>Hello &lt;World&gt;</a>', translate(:'translations.interpolated_html', :word => '<World>')
assert_equal '<a>Hello &lt;World&gt;</a>', translate(:'translations.interpolated_html', :word => stub(:to_s => "<World>"))
assert_equal '<a>Hello &lt;World&gt;</a>', translate(:'translations.interpolated_html', :word => word_struct.new("<World>"))
end
def test_translate_with_html_count