cucumber (LoadError) for my model - ruby-on-rails-3

I'm sure this is basic as I'm a new Rails user and want to do Cucumber right. I spent the weekend reading the Pragmatic book and have a small project I want to create and use it. I intentionally set up my first feature as broadly as possible so that it wouldn't be brittle depending too much on how it works on the rails side. I created a new rails app using DanielKehoe's starter on github. I think I got the user figured out. But when I attempt to use Cucumber to creating my first table of Reference Units which would be a table of constants, I expected that cucumber would drive me to create a Reference Units model but I spent all morning trying to get it to do so using the book, Railscasts and stackoverflow to push me to where I'm at now. I went ahead and generated a model for Reference Units, controller with a new action and an empty view. But it now cannot figure out that I have Reference Unit model with a (LoadError). Odd because it's there in the app.
Here's my current feature:
Feature: I want to have Reference Units that I can refer to so they can be used elsewhere. That way
they can be updated in one place. I want to create and edit these Units.
Background:
Given I am logged in as the following user:
| name | "Testy McUserton" |
| password | "please" |
| email | "testy#userton.com" |
Scenario: Adding Reference Units
When I go to the new Reference Units page
And I fill in the following:
| commodity | "corn" |
| language | "en" |
| wholesale unit | "xton" |
| retail unit | "xliter" |
| receipt unit | "dollar" |
Then it should create a new Reference Unit
here is my step definition:
Given /^I am logged in as the following user:$/ do |table|
sign_up valid_user
end
When /^I go to the new Reference Units page$/ do
visit new_reference_unit_path
end
When /^I fill in the following:$/ do |table|
#reference_unit = Reference_unit.create!(table.rows_hash)
end
Then /^it should create a new Reference Unit$/ do
pending # express the regexp above with the code you wish you had
end
new_reference_unit_path is in my feature/support/paths.rb as:
when /the new Reference Units page/
'/reference_units/new'
Running this in 3.1.3 the following is in red:
Scenario: Adding Reference Units # features/user_can_create_units.feature:10
When I go to the new Reference Units page # features/step_definitions/user_create_unit_reference.rb:6
Expected /Users/sam/apps/keriakoo/app/models/reference_unit.rb to define Reference_unit (LoadError)
./app/controllers/reference_units_controller.rb:4:in `new'
./features/step_definitions/user_create_unit_reference.rb:7:in `/^I go to the new Reference Units page$/'
features/user_can_create_units.feature:11:in `When I go to the new Reference Units page'
As I mentioned above. the reference_unit.rb file is in the correct place. Of course, I haven't added any methods or attributes to it expecting cucumber to guide me at the right time. I'm sure it's something simple, but I tried all kinds of combinations of words and capitalization to shake something loose, sam

OK, I think I hit on the clue. Digging around, I used the --backtrace option on cucumber and it pointed me to a dependency with a key word of "const". OK, it doesn't like something in the text. Looking around for info on two-word models, I can tell that my model was correct: ReferenceUnit. But looking at my error output, it was looking for Reference_unit. So for laughs I altered the regex for that step to be "When /^I go to the new Reference units page$" the error went away. So the syntax of the first line is important, and not 'freestylin' as I suspected.

Related

How to use the scenario as prerequisite of another scenario

first feature file
Feature: CRMSmokeTest
In order to make sure that CRM Key functionalities working as expected.
Background:
Given I have entered the CRM URL
Scenario Outline:Quick Search using AccountID
Given AccountID is selected in The Quick Search
When user enter the <AccountID> in search field
And Click on Quick Search button
And Close the Alerts
Then Title of the page contains <AccountID>
Examples:
| AccountID |
| 116999 |
Second Feature File
Feature: CRM Ticket Open, Add and Amend
In order to verify thay user able to open and amend existing ticket
Also to verify that user is able to create a new Ticket
Background:
Given I have entered the CRM URL
And AccountID is selected in The Quick Search
#mytag
Scenario Outline: Add a new Ticket
When user enter the <AccountID> in search field
And Click on Quick Search button
And Close the Alerts
Then Title of the page contains <AccountID>
When User click on Add New link on Ticket Section
And Select the <Departmnet> and <SubTeam> from the list
And Enter the <Subject> of the ticket
And Select the <Product>
And Select the <TicketCategory> and <TicketSubCategory>
And Enter the <Comments> and <PersonSpokeTo>
And Click on Finish
Then A new Ticket is created
Examples:
| AccountID | Department | SubTeam | Subject | Product | TicketCategory | TicketSubCategory | Comments |
| 116999 | Customer Services | ContractEnquiry | Test Ticket | Home Insurance | Account Management | Customer Zone | Test Comments |
I would like to use the Scenario in my first feature file as the prereq of my scenario in second feature file.
What is the best practice to so
Also When filling a big data form what is the best approach to write scenario. The way I have written the scenario in second feature file is the only approach or we can write this better way?
Calling a different scenario because it satisfies the prerequisites of the current scenario breaks the isolation required to make each scenario runnable on its own. No scenario should rely on any other scenario.
Instead of copying and pasting the steps from the first scenario, write a short Given step that performs the same things as the first scenario.
Judging on the scenario title, create a Given step similar to:
Scenario Outline: Add a new Ticket
# New 'Given' step that basically does the same thing as scenario #1
Given user performed a quick search for account <AccountID>
# Now continue on with the rest of the scenario
When User click on Add New link on Ticket Section
And Select the <Departmnet> and <SubTeam> from the list
And Enter the <Subject> of the ticket
And Select the <Product>
And Select the <TicketCategory> and <TicketSubCategory>
And Enter the <Comments> and <PersonSpokeTo>
And Click on Finish
Then A new Ticket is created
Examples:
| AccountID | Department | SubTeam | Subject | Product | TicketCategory | TicketSubCategory | Comments |
| 116999 | Customer Services | ContractEnquiry | Test Ticket | Home Insurance | Account Management | Customer Zone | Test Comments |
The implementation of this step will depend on the architecture of your tests, but the step should:
Go to the CRM URL
Select AccountId in the quick search
Enter the given Account Id in the search box
Click the quick search button
Close the alerts when the show up
If you find yourself writing code that seems to exist in your other steps consider refactoring your code into Page Models, and then initializing those page models and calling methods on them from your step definitions. The basic control flow of your test will go:
Feature file --> Step definition --> Page model --> Selenium --> Web browser

Karate API Testing - Access variable value across scenario in a feature file

I am running into this situation (similar to this) when trying to do the following:
Scenario: Create User
Given path 'user'
And request read('user.json');
When method post
Then status 200
And def user_id = response.userId
Scenario: Test A
Given path 'user/' + user_id <- Received javascript error here
...
Scenario: Test B
Given path 'user/' + user_id <- Received javascript error here
...
Scenario: Test A
Given path 'user/' + user_id <- Received javascript error here
...
Basically what i am trying to do is create a user in my database first, then run it through a series of test and use a last scenario to delete this user. Therefore, i need to share the user_id value across multiple scenarios. Background doesn't work for me because that means i have to create a new user for each scenario. I have seen in the demo that a simple way would be to put all test under 1 scenario but i don't feel that it is right to put all tests on 1 scenario
I have reviewed the karate demo but i did not come across any sample code that will help my situation. May i know the right way to do this in Karate? Thanks.
I think you are missing the callonce keyword. Understand it and then look at the demos, for example, this one: callonce.feature.
You will need to move the 'common' code into a separate feature, but that is the right thing to do, because you will typically want it re-usable by multiple feature files as well.

How can I test that an image has been sent to a user on a chat application

I've got a chat application I'm doing Gherkin for and one of the features I'll be adding is the ability to send images of various types, I just don't know how to write this up. Could it be?
Given the following users exist:
| Username |
| Alice |
| Bob |
When "Alice" sends "Bob" "Image A"
Then "Bob" can see "Image A"
Is "Image A" the url to the image? And, if it is, where does the image reside for the test itself?
This is a common programming problem where you need to store image location in a variable and pass that variable as argument. You can pass that variable name from gherkin feature file. See Here for storing file location, hope this helps:
How to pass a text file as a argument?

Given/When/Then deprecated in Cucumber

I'm getting the following warning with this code:
WARNING: Using 'Given/When/Then' in step definitions is deprecated, use 'step' to call other steps instead
How can I correct this?
Code:
Feature: Viewing tickets
In order to view the tickets for a project
As a user
I want to see them on that project's page
Background:
Given there is a project called "TextMate 2"
And that project has a ticket:
| title | description |
| Make it shiny! | Gradients! Starbursts! Oh my! |
And there is a project called "Internet Explorer"
And that project has a ticket:
| title | description |
| Standards compliance | Isn't a joke. |
And I am on the homepage
Scenario: Viewing tickets for a given project
When I follow "TextMate 2"
Then I should see "Make it shiny!"
And I should not see "Standards compliance"
When I follow "Make it shiny!"
Then I should see "Make it shiny" within "#ticket h2"
And I should see "Gradients! Starbursts! Oh my!"
When I am on the homepage
And I follow "Internet Explorer"
Then I should see "Standards compliance"
And I should not see "Make it shiny!"
When I follow "Standards compliance"
Then I should see "Standards compliance" within "#ticket h2"
And I should see "Isn't a joke.
Thanks a lot!
Somewhere in step definitions you're using the following construction:
Then "a file named '#{want_file}' should exist"
instead you should use
step("a file named '#{want_file}' should exist")
or (preferred I think) - avoid calling one step from another at all. It is better to refactor definitions and extract common part into separate class or method.
replace this (features/step_definitions/web_steps.rb, line 35-37):
When /^(.*) within (.*[^:])$/ do |step, parent|
with_scope(parent) { When step }
end
with something like this:
When /^(.*) within (.*[^:])$/ do |the_step, parent|
with_scope(parent) { step the_step }
end
worked for me !

Do the blue dashes in cucumber features output always mean skipped?

I've understood that blue dashes in features output means the step was skipped because something before it failed but in all of my scenario outlines I get blue dashes but also a line that says all passed.
Here is my Scenario Outline.
Scenario Outline: Attempt to assign a role when not authorized
Given a <user_who_can_not_assign_roles> exists with email: "johndoe#example.com"
And that user is signed in
And I am on the user's show page
And a role exists with name: "<other_role1>"
And a role exists with name: "<other_role2>"
When I follow "Edit"
Then I should not see "Admin"
And I should not see "Manager"
And I should not see "Salesperson"
When I fill in "username" with "spuds"
And I fill in "password" with "potatoes"
And I fill in "password confirmation" with "potatoes"
And I fill in "email" with "spuds#gmail.com"
And I press "Save"
Then I should see "success"
And a role should exist with name: "<other_role1>"
And that role should not be one of the user's roles
And a role should exist with name: "<other_role2>"
And that role should not be one of the user's roles
Examples:
| user_who_can_not_assign_roles | other_role1 | other_role2 |
| manager | Admin | Salesperson |
| salesperson | Admin | Manager |
When I run this feature I get this output.
-------------------......
2 scenarios (2 passed)
38 steps (38 passed)
0m3.300s
I get that its 2 scenarios because I have 2 examples listed and 38 steps would be the 19 run twice. What I don't understand is why it shows the blue dashes (like it normally shows for skipped steps) when it also says 38 steps passed.
I would assume that this is intended when running outlines because if I change on of the steps marked with a blue dash it will show failed. I was just looking for some confirmation in the cucumber docs and I can't find anything.
I'm running rails 3.0.0, cucumber 0.9.3, and pickle 0.4.2.
The blue dashes in this case represent the parsing of the scenario outline, which is more metadata than test. I find it confusing as well. To get a better idea of what is going on, try executing:
cucumber -f pretty <your_fancy_scenario.feature>
This will force cucumber to display the actual scenario text with the color coding, instead of just the dots and dashes.
Hope that helps!