How to spec views

I’m trying to spec a view but haven’t done much view specing.

This view render different partials depending on authentication of the
user:
annon, admin, player
So I I’ll write if conditionals in the view with the partials

it “should render signup propaganda for annon users trying to view
games”
do
render “/games/index.rhtml”
@logged_in?.should eql(false)
response.should render_template(’_signup_propaganda’)
end

Now for my partial I know it’ll be wrapped all in a div with a
class=“signup_propaganda”
Should I be testing for that instead? Can I write expectations for
partials
similar to above?

When your specing views are you testing for the outputted results?

it “should render signup propaganda for annon users trying to view
games”
do
render “/games/index.rhtml”
@logged_in?.should eql(false)
response.should have_tag(div, “class=/“signup_propaganda/””)
end

How should I be writing my spec?

On 10/1/07, Andrew WC Brown [email protected] wrote:

@logged_in?.should eql(false)

it “should render signup propaganda for annon users trying to view games”
do
render “/games/index.rhtml”
@logged_in?.should eql(false)
response.should have_tag(div, “class=/“signup_propaganda/””)
end

How should I be writing my spec?

There is no simple answer to your question. If anyone offers you one,
treat it with a grain of salt.

Coding by example is a process. If you’re doing it right, the examples
are going to change as you progress. So in this case, I might start
like this:

it “should render signup propaganda for annon users trying to view
games” do
controller.stub!(:logged_in?).and_return(false)
render “/games/index.rhtml”
response.should have_tag(‘div.signup_propaganda’)
end

The code to make this pass could just be:

At this point I’d want to add an example about what a logged in user
sees to force the conditional:

it “should NOT render signup propaganda for logged in users trying to
view games” do
controller.stub!(:logged_in?).and_return(true)
render “/games/index.rhtml”
response.should_not have_tag(‘div.signup_propaganda’)
end

leading to this code:

<% if logged_in? %>

<% end %>

At some point down the line I might decide to extract the div to a
partial. At that point, I should be able to do so without changing
the example. Once the partial has been extracted, then comes the
question about what to do with the example, and the answer will depend
on a few things.

If the partial is only ever used in this one template, and requires no
additional setup, and the only reason I extracted it was to clean up
the template, I might leave things as/is.

Most of the time, however, I’d change the examples to expect that the
partial gets rendered. First, I’d create a new example for the partial
itself and move anything from the old example that was specific to the
content inside that partial. Only after that’s done and all examples
are passing, I’d change the original examples to look like this:

it “should render signup propaganda for annon users trying to view
games” do
controller.stub!(:logged_in?).and_return(false)
template.expect_render(:partial => ‘signup_propoganda’)
render “/games/index.rhtml”
end

it “should NOT render signup propaganda for logged in users trying to
view games” do
controller.stub!(:logged_in?).and_return(true)
template.expect_render(:partial => ‘signup_propoganda’).never
render “/games/index.rhtml”
end

HTH,
David

The was really helpful, thanks David!

“There is no simple answer to your question. If anyone offers you one,
treat it with a grain of salt.”

The game I’m specing actually has an attribute called grains_of_salt.
No Lie.

It didn’t know what controller was, should it not know it what it is by
default or do I have to assign a controller at the top of my spec?

On 10/1/07, Andrew WC Brown [email protected] wrote:

It didn’t know what controller was, should it not know it what it is by
default or do I have to assign a controller at the top of my spec?

Try template instead, or @controller.

The controller used in view specs is a generic controller that ships
w/ rspec_on_rails, not the controller that is mapped to the view.

On 10/1/07, Andrew WC Brown [email protected] wrote:

ActionView::TemplateError in ‘/games/index.rhtml should render a list of
games for authenticated users’
undefined local variable or method `new_games_path’ for
#<#Class:0x32bd2a4:0x32bb5bc>

I am I suppose to stub the named route somehow?
since a link_to generates an anchor tag shouldn’t of my spec have passed?

What version of rspec/rspec_on_rails are you using?

How about spec’ing links?

<%= link_to ‘Create new game’, new_games_path %>

it “should have a create games link for admin” do
template.stub!(:logged_in?).and_return(true)
template.stub!(:admin?).and_return(true)
template.should have_tag(‘a’,‘Create new game’)
render “/games/index.rhtml”
end

It says that it didn’t show up

‘/games/index.rhtml should have a create games link for admin’ FAILED
Expected at least 1 elements, found 0.
is not true.
./spec/views/games/index.rhtml_spec.rb:70:

Also all specs have a problem with the named route

ActionView::TemplateError in ‘/games/index.rhtml should render a list of
games for authenticated users’
undefined local variable or method `new_games_path’ for
#<#Class:0x32bd2a4:0x32bb5bc>

I am I suppose to stub the named route somehow?
since a link_to generates an anchor tag shouldn’t of my spec have
passed?

1.0.9

On 10/1/07, Andrew WC Brown [email protected] wrote:

1.0.9

That’s not been released, so you must be working from trunk. I don’t
think, however, you have the latest trunk because I think this has
been fixed.

Try updating (per
http://rspec.rubyforge.org/documentation/rails/install.html near the
bottom) and see if this problem goes away.

Cheers,
David

Well I figured out why it didn’t understand what the route was.

it was new_games_path when it should have been new_game_path.

Still not sure about the anchor tag

On 10/1/07, Andrew WC Brown [email protected] wrote:

That’s not been released, so you must be working from trunk. I don’t

template.should have_tag(‘a’,‘Create new game’)
./spec/views/games/index.rhtml_spec.rb:70:

It didn’t know what controller was, should it not know it what
view.

specing.

it "should render signup propaganda for annon users

outputted

  response.should have_tag(div,

Coding by example is a process. If you’re doing it right,
games" do
logged in

response.should_not have_tag(‘div.signup_propaganda’ )
a

If the partial is only ever used in this one template, and
the

are passing, I’d change the original examples to look like

rspec-users mailing list

http://rubyforge.org/mailman/listinfo/rspec-users


Monsterbox Productions
putting small businesses on-line

1319 Victoria Avenue East
Thunder Bay, Ontario P7C 1C3
Canada

Andrew WC Brown
web-developer and owner
[email protected]
P: 807-626-9009
F: 807-624-2705

I solved it.

The two problems were that I named my paths wrong and I was calling
template
instead of repsonse

it “should have a create games link for admin” do
template.stub!(:logged_in?).and_return(true)
template.stub!(:admin?).and_return(true)
render “/games/index.rhtml”
response.should have_tag(‘a’,‘Create new game’)
end

I’m stlll getting used to the whole spec’ing view things.
Its good to know my only problem is minor typo’s

I love RSPEC

Specing Image Tags?

it “should show a propaganda image” do
render “/games/index.rhtml”
response.should have_tag(‘img’,‘signup_propaganda.gif’)
end

The following doesn’t work. Im guess have_tag isnt the best approach to
test
them.
Should I just use have_text and look for the img and
signup_propaganda.gif?

I had reinstalled the plugin yesterday but I reinstalled it and its
revision
2680
It still gives me the error.

On 10/1/07, Andrew WC Brown [email protected] wrote:

Specing Image Tags?

it “should show a propaganda image” do
render “/games/index.rhtml”
response.should have_tag(‘img’,‘signup_propaganda.gif’)
end

have_tag expect ‘tag’ and ‘content’…

There was a previous discussion on how to assert on HTML element
attributes.

http://rubyforge.org/pipermail/rspec-users/2007-September/003205.html


Luis L.
Multimedia systems

Leaders are made, they are not born. They are made by hard effort,
which is the price which all of us must pay to achieve any goal that
is worthwhile.
Vince Lombardi

On 10/1/07, Andrew WC Brown [email protected] wrote:

Specing Image Tags?

it “should show a propaganda image” do
render “/games/index.rhtml”
response.should have_tag(‘img’,‘signup_propaganda.gif’)
end

The following doesn’t work. Im guess have_tag isnt the best approach to test
them.
Should I just use have_text and look for the img and signup_propaganda.gif?

have_tag wraps assert_select, which is part of the rails testing API.
You can find docs on it here:

http://api.rubyonrails.com/classes/ActionController/Assertions/SelectorAssertions.html#M000208

For your example you can do this:

response.should have_tag(‘img[src=?]’,‘signup_propaganda.gif’)

Cheers,
David

I think I asked to quickly.
I’m guessing its

template.expect_render(:partial => ‘games’, :collection => @games)

On 10/1/07, Andrew WC Brown [email protected] wrote:

end

render “/games/index.rhtml”
On 10/1/07, Andrew WC Brown < [email protected] > wrote:
its
don’t

Cheers,

it “should have a create games link for admin” do

of
passed?
know it
that ships

"There is no simple answer to your question. If

specing.

response.should

for
users trying

How should I be writing my spec?
examples

games" do

to

without

If the partial is only ever used in this one
Most of the time, however, I’d change the
specific

it "should render signup propaganda for annon

end
http://rubyforge.org/mailman/listinfo/rspec-users

[email protected]


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users


Monsterbox Productions
putting small businesses on-line

1319 Victoria Avenue East
Thunder Bay, Ontario P7C 1C3
Canada

Andrew WC Brown
web-developer and owner
[email protected]
P: 807-626-9009
F: 807-624-2705

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs