GithubHelp home page GithubHelp logo

Accepting method arguments about rspec-its HOT 15 CLOSED

rspec avatar rspec commented on June 30, 2024
Accepting method arguments

from rspec-its.

Comments (15)

d-Pixie avatar d-Pixie commented on June 30, 2024 1

Nice trick to accomplish this with the gem as is:

RSpec.describe Range do
  let(:range) { described_class.new( 1, 10 )}

  describe '#cover?' do
    subject { range.method(:cover?) }

    its([2]) { is_expected.to eq true }
    its([1..4]) { is_expected.to eq true }
  end
end

Unfortunately not my idea 🙂 All credit goes to https://zverok.github.io/blog/2017-11-01-rspec-method-call.html

from rspec-its.

ArturT avatar ArturT commented on June 30, 2024

I like it. Many times I felt lack of that feature. Would be great to have it.

from rspec-its.

BenMorganIO avatar BenMorganIO commented on June 30, 2024

I'm +1 for #its carrying multiple arguments.

from rspec-its.

michael-harrison avatar michael-harrison commented on June 30, 2024

+1 :)

from rspec-its.

stp-che avatar stp-che commented on June 30, 2024

I would like to have the second #its argument as a description overriding rather than the argument for tested method.

I mean I want to write like this

Person = Struct.new(:name)

describe Person do
  subject { Person.new("Jack") }
  its(:name, 'should be taken from initialization argument') { should == "Jack" }
end

so that it would generate the following documentation output:

Person
  name
    should be taken from initialization argument

from rspec-its.

vamereh avatar vamereh commented on June 30, 2024

@stp-che 👍

from rspec-its.

ArturT avatar ArturT commented on June 30, 2024

I think multiple arguments feature for tested method is more needed. We can also add as a last argument of its the description but what if tested method takes multiple arguments.

from rspec-its.

BenMorganIO avatar BenMorganIO commented on June 30, 2024

We could use a hash as a parameter. There could be a methods key with an
array or a single object as an input. For a description override, we could
have a should key.

On Thursday, August 14, 2014, Artur [email protected] wrote:

I think multiple arguments feature for tested method is more needed. We
can also add as a last argument of its the description but what if tested
method takes multiple arguments.


Reply to this email directly or view it on GitHub
#19 (comment).

from rspec-its.

stp-che avatar stp-che commented on June 30, 2024

A hash, indeed, could solve the problem.
I imagine something like this:

its(:method, with: [arg1, arg2], should: 'return a value'){ ... }

from rspec-its.

ArturT avatar ArturT commented on June 30, 2024

@stp-che +1 It looks nice. :)

from rspec-its.

dnagir avatar dnagir commented on June 30, 2024

Just FYI guys.

There's a gem that does just this: https://github.com/dnagir/its

Although it hasn't been updated for a whilte I regularly use it in all my projects and it seems to be solid.

But TBH I would really love it to be part of rspec-it.

from rspec-its.

jeremyf avatar jeremyf commented on June 30, 2024

👍 This looks very nice.

from rspec-its.

palfvin avatar palfvin commented on June 30, 2024

My apologies for not responding to this or any other issues/PRs for the last several months. I'll try to be better going forward.

That said, I'm inclined not to support this on the grounds that:

  • The its method has historically been in place to support "attributes", i.e. argument-less methods which can be thought of as returning some "property" of the object, rather than arbitrary methods which "do something". Introducing support for arbitrary methods seems like it's heading down a slippery support of incorporating more general support for expectations.
  • I'd rather see additional arguments be used to provide support for other RSpec cababilities (e.g. metadata).

I see that others have supported this extension, but I'd like to hear from anyone who might be opposed.

from rspec-its.

DArrigoni avatar DArrigoni commented on June 30, 2024

Personally, I probably would not use this and would end up ignoring it. I agree that "its" semantically feels appropriate for properties rather than method calls.

That said, a corollary that might work would be something like a 'call' or 'send' method, so it would look like...

call(:some_method, with: [an_arguement, another_argument]) { should pass_some_assertion }

Such a thing seems beyond the scope of this project however.

from rspec-its.

pirj avatar pirj commented on June 30, 2024

Another option that doesn't involve rspec-its:

describe '#root' do
  def root(x)
    MathExample.new.root(x)
  end

  it { expect(root(16)).to eq 4 }
  it { expect(root(9)).to eq 3 }
end
RSpec.describe Range do
  subject(:range) { described_class.new( 1, 10 )}

  describe '#cover?' do
    def cover?(range_or_value)
      range.cover?(range_or_value)
    end

    it { expect(cover?(2)).to be(true) }
    it { expect(cover?(1..4)).to be(true) }
  end
end

All credit goes to the Effective Testing with RSpec 3 book.

from rspec-its.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.