Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update rspec should to expect via transpec #215

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
98 changes: 49 additions & 49 deletions spec/cocoon_spec.rb
Expand Up @@ -20,7 +20,7 @@ class TestClass < ActionView::Base

context "link_to_add_association" do
before(:each) do
@tester.stub(:render_association).and_return('form<tag>')
allow(@tester).to receive(:render_association).and_return('form<tag>')
end

context "without a block" do
Expand All @@ -43,24 +43,24 @@ class TestClass < ActionView::Base

context "and explicitly specifying the wanted partial" do
before do
@tester.unstub(:render_association)
@tester.should_receive(:render_association).with(anything(), anything(), anything(), "f", anything(), "shared/partial").and_return('partiallll')
allow(@tester).to receive(:render_association).and_call_original
expect(@tester).to receive(:render_association).with(anything(), anything(), anything(), "f", anything(), "shared/partial").and_return('partiallll')
@html = @tester.link_to_add_association('add something', @form_obj, :comments, :partial => "shared/partial")
end

it_behaves_like "a correctly rendered add link", {template: "partiallll"}
end

it "gives an opportunity to wrap/decorate created objects" do
@tester.unstub(:render_association)
@tester.should_receive(:render_association).with(anything(), anything(), kind_of(CommentDecorator), "f", anything(), anything()).and_return('partiallll')
allow(@tester).to receive(:render_association).and_call_original
expect(@tester).to receive(:render_association).with(anything(), anything(), kind_of(CommentDecorator), "f", anything(), anything()).and_return('partiallll')
@tester.link_to_add_association('add something', @form_obj, :comments, :wrap_object => Proc.new {|comment| CommentDecorator.new(comment) })
end

context "force non association create" do
context "default case: create object on association" do
before do
@tester.should_receive(:create_object).with(anything, :comments , false)
expect(@tester).to receive(:create_object).with(anything, :comments , false)
@html = @tester.link_to_add_association('add something', @form_obj, :comments)
end

Expand All @@ -69,15 +69,15 @@ class TestClass < ActionView::Base

context "and explicitly specifying false is the same as default" do
before do
@tester.should_receive(:create_object).with(anything, :comments , false)
expect(@tester).to receive(:create_object).with(anything, :comments , false)
@html = @tester.link_to_add_association('add something', @form_obj, :comments, :force_non_association_create => false)
end
it_behaves_like "a correctly rendered add link", {}
end

context "specifying true will not create objects on association but using the conditions" do
before do
@tester.should_receive(:create_object).with(anything, :comments , true)
expect(@tester).to receive(:create_object).with(anything, :comments , true)
@html = @tester.link_to_add_association('add something', @form_obj, :comments, :force_non_association_create => true)
end
it_behaves_like "a correctly rendered add link", {}
Expand Down Expand Up @@ -127,8 +127,8 @@ class TestClass < ActionView::Base

context "and explicitly specifying the wanted partial" do
before do
@tester.unstub(:render_association)
@tester.should_receive(:render_association).with(anything(), anything(), anything(), "f", anything(), "shared/partial").and_return('partiallll')
allow(@tester).to receive(:render_association).and_call_original
expect(@tester).to receive(:render_association).with(anything(), anything(), anything(), "f", anything(), "shared/partial").and_return('partiallll')
@html = @tester.link_to_add_association( @form_obj, :comments, :class => 'floppy disk', :partial => "shared/partial") do
"some long name"
end
Expand Down Expand Up @@ -157,13 +157,13 @@ class TestClass < ActionView::Base
end

it "tttt" do
@post.class.reflect_on_association(:people).klass.new.should be_a(Person)
expect(@post.class.reflect_on_association(:people).klass.new).to be_a(Person)
end

context "with extra render-options for rendering the child relation" do
context "uses the correct plural" do
before do
@tester.should_receive(:render_association).with(:people, @form_obj, anything, "f", {:wrapper => 'inline'}, nil)
expect(@tester).to receive(:render_association).with(:people, @form_obj, anything, "f", {:wrapper => 'inline'}, nil)
@html = @tester.link_to_add_association('add something', @form_obj, :people, :render_options => {:wrapper => 'inline'})
end
it_behaves_like "a correctly rendered add link", {association: 'person', associations: 'people' }
Expand All @@ -173,18 +173,18 @@ class TestClass < ActionView::Base
context "passing locals to the partial" do
context "when given: passes the locals to the partials" do
before do
@tester.unstub(:render_association)
@form_obj.should_receive(:fields_for) { | association, new_object, options_hash, &block| block.call }
@tester.should_receive(:render).with("person_fields", {:f=>nil, :dynamic=>true, :alfred=>"Judoka"}).and_return ("partiallll")
allow(@tester).to receive(:render_association).and_call_original
expect(@form_obj).to receive(:fields_for) { | association, new_object, options_hash, &block| block.call }
expect(@tester).to receive(:render).with("person_fields", {:f=>nil, :dynamic=>true, :alfred=>"Judoka"}).and_return ("partiallll")
@html = @tester.link_to_add_association('add something', @form_obj, :people, :render_options => {:wrapper => 'inline', :locals => {:alfred => 'Judoka'}})
end
it_behaves_like "a correctly rendered add link", {template: 'partiallll', association: 'person', associations: 'people' }
end
context "if no locals are given it still works" do
before do
@tester.unstub(:render_association)
@form_obj.should_receive(:fields_for) { | association, new_object, options_hash, &block| block.call }
@tester.should_receive(:render).with("person_fields", {:f=>nil, :dynamic=>true}).and_return ("partiallll")
allow(@tester).to receive(:render_association).and_call_original
expect(@form_obj).to receive(:fields_for) { | association, new_object, options_hash, &block| block.call }
expect(@tester).to receive(:render).with("person_fields", {:f=>nil, :dynamic=>true}).and_return ("partiallll")
@html = @tester.link_to_add_association('add something', @form_obj, :people, :render_options => {:wrapper => 'inline'})
end
it_behaves_like "a correctly rendered add link", {template: 'partiallll', association: 'person', associations: 'people' }
Expand All @@ -196,9 +196,9 @@ class TestClass < ActionView::Base
context "overruling the form parameter name" do
context "when given a form_name it passes it correctly to the partials" do
before do
@tester.unstub(:render_association)
@form_obj.should_receive(:fields_for) { | association, new_object, options_hash, &block| block.call }
@tester.should_receive(:render).with("person_fields", {:people_form => nil, :dynamic=>true}).and_return ("partiallll")
allow(@tester).to receive(:render_association).and_call_original
expect(@form_obj).to receive(:fields_for) { | association, new_object, options_hash, &block| block.call }
expect(@tester).to receive(:render).with("person_fields", {:people_form => nil, :dynamic=>true}).and_return ("partiallll")
@html = @tester.link_to_add_association('add something', @form_obj, :people, :form_name => 'people_form')
end
it_behaves_like "a correctly rendered add link", {template: 'partiallll', association: 'person', associations: 'people' }
Expand All @@ -208,30 +208,30 @@ class TestClass < ActionView::Base

context "when using formtastic" do
before(:each) do
@tester.unstub(:render_association)
@form_obj.stub(:semantic_fields_for).and_return('form<tagzzz>')
allow(@tester).to receive(:render_association).and_call_original
allow(@form_obj).to receive(:semantic_fields_for).and_return('form<tagzzz>')
end
context "calls semantic_fields_for and not fields_for" do
before do
@form_obj.should_receive(:semantic_fields_for)
@form_obj.should_receive(:fields_for).never
expect(@form_obj).to receive(:semantic_fields_for)
expect(@form_obj).to receive(:fields_for).never
@html = @tester.link_to_add_association('add something', @form_obj, :people)
end
it_behaves_like "a correctly rendered add link", {template: 'form<tagzzz>', association: 'person', associations: 'people' }
end
end
context "when using simple_form" do
before(:each) do
@tester.unstub(:render_association)
@form_obj.stub(:simple_fields_for).and_return('form<tagxxx>')
allow(@tester).to receive(:render_association).and_call_original
allow(@form_obj).to receive(:simple_fields_for).and_return('form<tagxxx>')
end
it "responds_to :simple_fields_for" do
@form_obj.should respond_to(:simple_fields_for)
expect(@form_obj).to respond_to(:simple_fields_for)
end
context "calls simple_fields_for and not fields_for" do
before do
@form_obj.should_receive(:simple_fields_for)
@form_obj.should_receive(:fields_for).never
expect(@form_obj).to receive(:simple_fields_for)
expect(@form_obj).to receive(:fields_for).never
@html = @tester.link_to_add_association('add something', @form_obj, :people)
end
it_behaves_like "a correctly rendered add link", {template: 'form<tagxxx>', association: 'person', associations: 'people' }
Expand All @@ -257,9 +257,9 @@ class TestClass < ActionView::Base
it "is rendered inside a input element" do
doc = Nokogiri::HTML(@html)
removed = doc.at('input')
removed.attribute('id').value.should == "Post__destroy"
removed.attribute('name').value.should == "Post[_destroy]"
removed.attribute('value').value.should == "false"
expect(removed.attribute('id').value).to eq("Post__destroy")
expect(removed.attribute('name').value).to eq("Post[_destroy]")
expect(removed.attribute('value').value).to eq("false")
end

it_behaves_like "a correctly rendered remove link", {}
Expand Down Expand Up @@ -287,9 +287,9 @@ class TestClass < ActionView::Base
it "is rendered inside a input element" do
doc = Nokogiri::HTML(@html)
removed = doc.at('input')
removed.attribute('id').value.should == "Post__destroy"
removed.attribute('name').value.should == "Post[_destroy]"
removed.attribute('value').value.should == "true"
expect(removed.attribute('id').value).to eq("Post__destroy")
expect(removed.attribute('name').value).to eq("Post[_destroy]")
expect(removed.attribute('value').value).to eq("true")
end

it_behaves_like "a correctly rendered remove link", {class: 'remove_fields dynamic destroyed'}
Expand Down Expand Up @@ -336,24 +336,24 @@ class TestClass < ActionView::Base

context "create_object" do
it "creates correct association with conditions" do
@tester.should_not_receive(:create_object_with_conditions)
expect(@tester).not_to receive(:create_object_with_conditions)
# in rails4 we cannot create an associated object when the object has not been saved before
# I submitted a bug for this: https://github.com/rails/rails/issues/11376
if Rails.rails4?
@post = Post.create(title: 'Testing')
@form_obj = double(:object => @post, :object_name => @post.class.name)
end
result = @tester.create_object(@form_obj, :admin_comments)
result.author.should == "Admin"
@form_obj.object.admin_comments.should be_empty
expect(result.author).to eq("Admin")
expect(@form_obj.object.admin_comments).to be_empty
end

it "creates correct association for belongs_to associations" do
comment = Comment.new
form_obj = double(:object => Comment.new)
result = @tester.create_object(form_obj, :post)
result.should be_a Post
comment.post.should be_nil
expect(result).to be_a Post
expect(comment.post).to be_nil
end

it "raises an error if cannot reflect on association" do
Expand All @@ -362,30 +362,30 @@ class TestClass < ActionView::Base

it "creates an association if object responds to 'build_association' as singular" do
object = Comment.new
object.should_receive(:build_custom_item).and_return 'custom'
@tester.create_object(double(:object => object), :custom_item).should == 'custom'
expect(object).to receive(:build_custom_item).and_return 'custom'
expect(@tester.create_object(double(:object => object), :custom_item)).to eq('custom')
end

it "creates an association if object responds to 'build_association' as plural" do
object = Comment.new
object.should_receive(:build_custom_item).and_return 'custom'
@tester.create_object(double(:object => object), :custom_items).should == 'custom'
expect(object).to receive(:build_custom_item).and_return 'custom'
expect(@tester.create_object(double(:object => object), :custom_items)).to eq('custom')
end

it "can create using only conditions not the association" do
@tester.should_receive(:create_object_with_conditions).and_return('flappie')
@tester.create_object(@form_obj, :comments, true).should == 'flappie'
expect(@tester).to receive(:create_object_with_conditions).and_return('flappie')
expect(@tester.create_object(@form_obj, :comments, true)).to eq('flappie')
end
end

context "get_partial_path" do
it "generates the default partial name if no partial given" do
result = @tester.get_partial_path(nil, :admin_comments)
result.should == "admin_comment_fields"
expect(result).to eq("admin_comment_fields")
end
it "uses the given partial name" do
result = @tester.get_partial_path("comment_fields", :admin_comments)
result.should == "comment_fields"
expect(result).to eq("comment_fields")
end
end

Expand Down
8 changes: 4 additions & 4 deletions spec/generators/install_generator_spec.rb
Expand Up @@ -13,18 +13,18 @@
context "in rails 3.0" do
context "with no arguments" do
before(:each) do
::Rails.stub(:version) { '3.0.8' }
allow(::Rails).to receive(:version) { '3.0.8' }
prepare_destination
run_generator
end

it "stubs the version correctly" do
::Rails.version[0..2].should == "3.0"
expect(::Rails.version[0..2]).to eq("3.0")
end

it "stubs the version correctly" do
test_version = (::Rails.version[0..2].to_f >= 3.1)
test_version.should be_false
expect(test_version).to be_false
end

it "copies cocoon.js to the correct folder" do
Expand All @@ -36,7 +36,7 @@
context "in rails 3.1" do
context "with no arguments" do
before(:each) do
::Rails.stub(:version) { '3.1.0' }
allow(::Rails).to receive(:version) { '3.1.0' }
prepare_destination
run_generator
end
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/navigation_spec.rb
Expand Up @@ -4,6 +4,6 @@
# include Capybara

it "should be a valid app" do
::Rails.application.should be_a(Dummy::Application)
expect(::Rails.application).to be_a(Dummy::Application)
end
end
22 changes: 11 additions & 11 deletions spec/support/shared_examples.rb
Expand Up @@ -17,24 +17,24 @@
@link = doc.at('a')
end
it 'has a correct href' do
@link.attribute('href').value.should == @options[:href]
expect(@link.attribute('href').value).to eq(@options[:href])
end
it 'has a correct class' do
@link.attribute('class').value.should == @options[:class]
expect(@link.attribute('class').value).to eq(@options[:class])
end
it 'has a correct template' do
@link.attribute('data-association-insertion-template').value.should == @options[:template]
expect(@link.attribute('data-association-insertion-template').value).to eq(@options[:template])
end
it 'has a correct associations' do
@link.attribute('data-association').value.should == @options[:association]
@link.attribute('data-associations').value.should == @options[:associations]
expect(@link.attribute('data-association').value).to eq(@options[:association])
expect(@link.attribute('data-associations').value).to eq(@options[:associations])
end
it 'has the correct text' do
@link.text.should == @options[:text]
expect(@link.text).to eq(@options[:text])
end
it 'sets extra attributes correctly' do
@options[:extra_attributes].each do |key, value|
@link.attribute(key).value.should == value
expect(@link.attribute(key).value).to eq(value)
end
end

Expand All @@ -56,17 +56,17 @@
@link = doc.at('a')
end
it 'has a correct href' do
@link.attribute('href').value.should == @options[:href]
expect(@link.attribute('href').value).to eq(@options[:href])
end
it 'has a correct class' do
@link.attribute('class').value.should == @options[:class]
expect(@link.attribute('class').value).to eq(@options[:class])
end
it 'has the correct text' do
@link.text.should == @options[:text]
expect(@link.text).to eq(@options[:text])
end
it 'sets extra attributes correctly' do
@options[:extra_attributes].each do |key, value|
@link.attribute(key).value.should == value
expect(@link.attribute(key).value).to eq(value)
end
end
end
Expand Down