简体   繁体   中英

Rspec expect assigns shorthand

I currently have something like:

it 'assigns @competition' do
  expect(assigns(:competition)).to be_a_new(Competition)
end

Is there a shorter version of this using the it { should ... } type syntax?

I don't know that it's shorter, but you can use:

subject {assigns(:competition)}
it {should be_a_new(Competition)}

or you can use:

it {expect(assigns(:competition)).to be_a_new(Competition)}

In both cases, the shortening is coming from the elimination of the string argument to it , which is independent of the use of should .

By now the RSpec documentation suggests to use is_expected.to , as in:

describe [1, 2, 3] do
  it { is_expected.to be_an Array }
  it { is_expected.not_to include 4 }
end

cf. http://www.rubydoc.info/gems/rspec-core/RSpec/Core/MemoizedHelpers#is_expected-instance_method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM