1

I would like to assert (in rspec really) that at least one item in the tags list is non-public.

it 'lets you select non-public tags' do
  get :new
  flag = false
  assigns(:tags).each do |tag|
    if tag.is_public == false
      flag = true
    end
  end
  flag.should eql true
end

What is a better, idiomatic way of doing the same?

4

2 回答 2

1

There are a million ways to do this:

# are any tags not public?
flag = assigns(:tags).any? { |tag| !tag.is_public }
flag.should eql true

or

# Are none of the tags public?
flag = assigns(:tags).none?(&:is_public)
flag.should eql true

or

# Find the first non-public tag?
flag = assigns(:tags).find { |tag| !tag.is_public}
flag.should_not eql nil
于 2013-05-24T19:56:43.413 回答
0

Lots of options here. Perhaps:

assigns(:tags).reject { |tag| tag.is_public }.should_not be_empty
于 2013-05-24T19:57:06.353 回答