# Rspec - In views

**URL:** <https://www.ruby-forum.com/t/rspec-in-views/178025>\
**Category:** Rails\
**Created:** [October 28, 2009, 6:30am UTC](https://www.ruby-forum.com/t/rspec-in-views/178025 "2009-10-28T06:30:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rails\_ROR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Rails\_ROR](https://www.ruby-forum.com/u/Rails_ROR)\
**Post date:** [October 28, 2009, 6:30am UTC](https://www.ruby-forum.com/t/rspec-in-views/178025/1 "2009-10-28T06:30:59Z")

</div>

Hi All,

I want to test my application with rspec.

In order to test my views, I have written few specs as given in the  
examples.

Is there any way to test the user acceptance?

i.e suppose the field takes a string.

I want to test it for integers, float values, alphanumeric values, etc  
and  
run tests against that.

I need to get the tests failed as it takes only string.

I would be really thankful if someone can guide me on this.

Please help me with a simple example so that I can understand it quickly  
and  
move ahead.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![Rails\_ROR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Rails\_ROR](https://www.ruby-forum.com/u/Rails_ROR)\
**Post date:** [October 28, 2009, 11:20am UTC](https://www.ruby-forum.com/t/rspec-in-views/178025/2 "2009-10-28T11:20:44Z")

</div>

Hi

But you haven’t given any example

---

<div class="post-metadata">

**Author:** ![Rails\_ROR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Rails\_ROR](https://www.ruby-forum.com/u/Rails_ROR)\
**Post date:** [October 28, 2009, 2:00pm UTC](https://www.ruby-forum.com/t/rspec-in-views/178025/3 "2009-10-28T14:00:33Z")

</div>

Hi,

Example if the view has a form with few fields and a submit button, I  
want  
to test whether the data that i enter is according to the database,i.e  
the  
model(Having all validations[Null]) and the page gets redirected to  
success  
page.

For Example:

require ‘spec\_helper’

describe “/admin/books/new.html.erb” do  
include Admin::BooksHelper

before(:each) do  
assigns[:book] = stub\_model(Book,  
:new\_record? =\> true,  
:sku\_number =\> “value for sku\_number”,  
:title =\> “value for title”,  
:author1 =\> “value for author1”,  
:author2 =\> “value for author2”,  
:author3 =\> “value for author3”,  
:author4 =\> “value for author4”,  
:author5 =\> “value for author5”

)  
end

it “renders new book form” do  
render

response.should have\_tag(“form[action=?][method=post]”,  
admin\_books\_path) do  
with\_tag(“input#book\_sku\_number[name=?]”, “book[sku\_number]”)  
with\_tag(“input#book\_title[name=?]”, “book[title]”)  
with\_tag(“input#book\_author1[name=?]”, “book[author1]”)  
with\_tag(“input#book\_author2[name=?]”, “book[author2]”)  
with\_tag(“input#book\_author3[name=?]”, “book[author3]”)  
with\_tag(“input#book\_author4[name=?]”, “book[author4]”)  
with\_tag(“input#book\_author5[name=?]”, “book[author5]”)

end  
end  
end

Now, i am testing whether the form renders to new book form. Similarly  
for  
each input box, i want to test my data with different values and also  
test  
whether the form has been rendered to show page after submitting this  
new  
form.

I would be thankful to you if you can guide me on this.

Thanks!!

---

<div class="post-metadata">

**Author:** ![Rails\_ROR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Rails\_ROR](https://www.ruby-forum.com/u/Rails_ROR)\
**Post date:** [October 28, 2009, 2:05pm UTC](https://www.ruby-forum.com/t/rspec-in-views/178025/4 "2009-10-28T14:05:37Z")

</div>

Rails ROR wrote:

> Hi,
> 
> Example if the view has a form with few fields and a submit button, I  
> want  
> to test whether the data that i enter is according to the database,i.e  
> the  
> model(Having all validations[Null]) and the page gets redirected to  
> success  
> page.

Views are dumb. What you want to test is actually controller  
functionality. And although you can do it with just RSpec, I’d  
recommend using Cucumber for anything at the feature or acceptance  
level.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Rails\_ROR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Rails\_ROR](https://www.ruby-forum.com/u/Rails_ROR)\
**Post date:** [October 28, 2009, 2:42pm UTC](https://www.ruby-forum.com/t/rspec-in-views/178025/5 "2009-10-28T14:42:11Z")

</div>

Rails ROR wrote:

> Hi All,
> 
> I want to test my application with rspec.
> 
> In order to test my views, I have written few specs as given in the  
> examples.
> 
> Is there any way to test the user acceptance?
> 
> i.e suppose the field takes a string.
> 
> I want to test it for integers, float values, alphanumeric values, etc  
> and  
> run tests against that.

These sound like unit tests for the model to me. As mentioned views are  
dumb (unless you’re talking about client-side JavaScript validation).  
What you really care about is whether invalid data can make it through  
your model validation.

> I need to get the tests failed as it takes only string.
> 
> I would be really thankful if someone can guide me on this.

The kinds of things you want to test in your view specs is whether the  
text field gets rendered, that it’s the right kind of control, has the  
right style class or id applied to it, that it has the right label, etc.

If you want to create specs for user acceptance then integration specs  
are the place to do that. As also mentioned; Cucumber provides a great  
way to provide those integration specs. Cucumber makes the acceptance  
test easy for the stakeholder to read and understand, which is vital to  
good user acceptance scripts.

---

<div class="post-metadata">

**Author:** ![Rails\_ROR](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9f8e36/32.png) [@Rails\_ROR](https://www.ruby-forum.com/u/Rails_ROR)\
**Post date:** [October 28, 2009, 2:12pm UTC](https://www.ruby-forum.com/t/rspec-in-views/178025/6 "2009-10-28T14:12:16Z")

</div>

2009/10/28 Rails ROR [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> Thanks in advance.
> 
> * * *
> 
> rspec-users mailing list  
> removed\_email\_address@domain.invalid  
> [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)

I would suggest buying a copy of the RSpec book (a beta PDF is available  
at  
([http://pragprog.com/](http://pragprog.com/)). This is by far the best resource for learning  
how to  
test with rspec and cucumber, and also is very up to date.

Andrew
