Ruby On Rails Classroom image

Pooja  Negi / Student / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3|Last
Lessons:- Tests for User Signup
$   rails  console
>> User.count
=>  0

 

Here User.count is 0 because we reset the database at the beginning of this section.

When submitting invalid data, we expect the user count not to change; when submitting valid data, we expect it to change by 1. We can express this in RSpec by combining the expect method with either the to method or the not_to method. We’ll start with the invalid case since it is simpler; we visit the signup path and click the button, and we expect it not to change the user count:

 

visit signup path
expect { click button   "Create my account" }.not to change ( User, :count)

 

Note that, as indicated by the curly braces, expect wraps click_button in a block (Section 4.3.2). This is for the benefit of the change method, which takes as arguments an object and a symbol and then calculates the result of calling that symbol as a method on the object both before and after the block. In other words, the code 

 expect { click button   "Create my account" }.not to change ( User, :count)

 

calculates


User.count 

 

before and after the execution of

not_to
click  button   "Create  my a ccount"

 

In the present case, we want the given code not to change the count, which we express using the not-to method. In effect, by enclosing the button click in a block we are able to replace

initial = User.count
click button  "Create my account"
final = User.count
initial.should == final

 

with the single line

 expect  {  click button  "Create my account" }.not to  change ( User :count)

 

which reads like natural language and is much more compact.
           The case of valid data is similar, but instead of verifying that the user count doesn’t change, we check that clicking the button changes the count by 1: 

 

 

This uses the to method because we expect a click on the signup button with valid data to change the user count by one. Combining the two cases with the appropriate describe blocks and pulling the common code into

          before blocks yields good basic tests for signing up users, as shown in Listing 7.16. Here we’ve factored out the common text for the submit button using the let method to define a submit variable.

 

 
 
 
image
Pooja   Negi

Skills    Ruby On Rails

Qualifications :- High School - SSN high school, College/University - HNBGU, College/University - SRHU,
Location :-Ranipokhari,Rishikesh,Uttarakhand,India
Description:- Student
Explore
 

  Students (0)