Same step using different step definitions Serenity Cucumber - bdd

I am automating a system using BDD with Serenity + Cucumber-jvm, and I have some cases, that the same step can be used as any keyword step definition.
Example:
Given something
When do something
Then other thing
And do something
As you can see, do something is used with both when and then, but If I define the same method with these 2 annotations:
#When("do something")
#Then("do something")
public void doSomething() {
}
I get the following error:
cucumber.runtime.DuplicateStepDefinitionException: Duplicate step definitions in ...
How do I resolve this issue

"Given" describes the context in which the scenario takes place. It either describes a state, or it describes something that has happened. So phrase it in either the continuous present, or the past tense.
Given the invoice is two weeks late <-- continuous present
Given the invoice was submitted <-- past
You'll also notice that it doesn't say anything about who submitted the invoice. That's fine for the context, because it doesn't matter how it got there. We call this the "passive" as opposed to the "active" voice.
"When" describes an event that happens. I like to put this in the present tense, active voice. "The dog bit the boy" is active voice. "The boy was bitten" is passive since it doesn't mention who did it. By mentioning who does it, we remind people that there's a user involved, even if that user is another system.
When I check my accounts
When the admin creates a new record
When the ETL begins
"Then" describes what ought to happen, in that context, for that event. I like to use the word "should", which is a conditional tense. It's also in the passive voice because the "when" should cover how it happened.
Then the invoice should be marked as paid
Then I should receive an email <-- "I" am not the doer so this is still passive voice
The word "should" has an additional benefit. Back in the days of Waterfall development, we used to try to get all the requirements right, but we never quite managed it.
By using the word "should", we encapsulate the idea that uncertainty still exists. It encourages people to question whether the requirements are accurate, and in the face of changing technology and innovation, whether they're still accurate. It's easier to change an idea when it isn't using "must" or "will", as those are words which express certainty, and if you're convinced that someone else is certain about something, you're less likely to push back.
By pushing back and making other suggestions we get "exploration by example", and it's the precursor to specification or test by example, which are nice by-products. Ideally you'll be talking through more scenarios than you actually keep, and deciding which ones are in and out of scope. The word "should" really helps with that, and keeping it when you capture those scenarios and write them down helps too.
And, of course, it helps to differentiate "Given" and "Then" (but hopefully not "When" since that will be phrased in the active voice anyway).
So, if I were to use your "do something" example, I might say:
Given something was done
When someone does something
Then something else should have happened.
Now none of your scenario steps are similar, and you've clarified what you really mean by them, too.
I wrote a blog post on this a while back if it helps as a reference.

For Cucumber The keyword in front a step definition (Given,When,Then,And..) does not make any difference it is for lexical clarity and beauty. That said when you do
#When("do something")
#Then("do something")
is equivalent to:
#When("do something")
#When("do something")
or
#Then("do something")
#Then("do something")
So not to get duplicate step definitions you need just to look at the text not the keyword :)

Related

Xtext - Temporarily tolerate unresolvable references and make the reference name user readable

Let's say I have a language that models a part of stackoverflow. Users are held in one resource, questions in another.
Users:
user fred : fred#foobar.com
user notfred : notfred#foobar.com
Questions:
question 123 by fred message "smart question"
question 124 by notfred message "not so smart question"
Now, the user "fred" wants to remove his account, but this wouldn't work because after loading both resources into my ResourceSet I would have a non-empty Resource#getErrors().
I can work around this by filtering XtextLinkingDiagnostic from the errors, but still other users reading the "smart question" cannot tell anymore that it was asked by someone called "fred". The info is still there, I can access it for example when I set a LinkingDiagnosticMessageProvider with LazyLinkingResource#setDiagnosticMessageProvider(...); however, the best thing I can now do is, show other users validation errors that "fred" was deleted, but they wouldn't know "fred" wrote the message they are just reading. Knowing this would help them a lot because everybody knows that "fred" writes great questions, right?
Long story short, I have an application into which users can load a declaration and a definition file. In very few cases something goes wrong and both files don't match perfectly, which means the definition has entries that are not declared in the declaration. However, I know that ~95% of the entries will still match!
Users cannot fix this quickly, but it is likely that they are happy just editing the 95% definitions, but they still need to be able to read the names of the 5% declarations without editing them!
I am not currently using any UI-parts of Xtext to edit the definitions, but rather a custom UI in form of a table. The current state with the missing declarations is that everything except a value column will be empty. The reference ID would be in another column, and knowing this ID would help the user a lot! Is there a clean way to achieve this?
Have a look at the 'Node Model' e.g. org.eclipse.xtext.nodemodel.util.NodeModelUtils.findNodesForFeature(EObject, EStructuralFeature) allows you to access the text that is written in the file

Some questions about building a network-accessible, multi-user, programmable, interactive environment

Introduction
I've been attempting to build this project for many weeks now, and trying multiple solutions that I can't get my head around. Let me describe the project a little. It's a text-based server, that players can login to (via telnet or a client), essentially like a MUD. They can then create and interact with 'objects', giving them 'verbs' and 'properties'.
The server is basically just a database of 'objects', each object has an ID, a name, a location (which is another object), a list of its contents (objects) and some other flags. Objects can have 'verbs' and 'properties'. Properties are just stored data (string, int, float, w/e). Verbs are methods/functions. Objects are interacted with using commands such as "put something in container". An old version of the server already exists, it's called LambdaMOO. I'm attempting to re-create it since it hasn't been updated in a very, very long time.
You can read more in-depth about how objects, verbs and properties should work at: http://bit.ly/17XIqjY
An Example
Let me describe what I'd like. Imagine we have an object. Object #256, it's called "Button". It has the property "count" along with all the default properties that are inherited from it's parent (i.e. 'description'). It has one "verb" on it, called "push". This verb contains this code:
this.count += 1;
this.description = "This button has been pushed " + this.count + " times.";
player.tell("You press the button and feel a chill run down your spine.");
When the player types 'push button' on the server, the 'push' verb will run and output
You press the button and feel a chill run down your spine.
If you then look at the button, you'll see it's updated description.
Note that player in the above script refers the object of the player executing the verb. tell is another verb, on the player object. However the tell verb has a flag saying it is executable from other verbs.
What language?
My main question is what languages can I use for the 'verbs'? I've tried using node.js and the 'vm' library. I've tried using C# to parse C#. I've tried using C# to parse JavaScript. The issue I keep getting is that I have no way of controlling the permissions of the verbs and properties. If I translate them to literal functions in JavaScript, I can't determine which object they are running on and what permissions it should have. If a user calls a function on another users object, I have no way of intercepting that call and stopping it if the permissions aren't correct. I'm not entirely fussed as to which language is used for the verb code it just needs to be "sandboxed". Properties need to be only readable/writeable when they are set to be so by the user, same with verbs. I imagine I could use a language with overloading (like PHP's __get, __set, __call).
I need to also be able to inject these variables into the verb: (mostly determined from the command typed, unless the verb is being called from another verb)
player (object) the player who typed the command
this (object) the object on which this verb was found
caller (object) this will be the same as ‘player’, unless another
verb calls the command in which case it is the object
containing that verb.
verb (string) the first word of the command
argstr (string) everything after the first word of the command
args (list of strings) a list of the words in ‘argstr’
dobjstr (string) the direct object string found during parsing
dobj (object) the direct object value found during matching
prepstr (string) the prepositional phrase found during parsing
iobjstr (string) the indirect object string
iobj (object) the indirect object value
I also need to be able to access any object from any other object (so long as the permissions work out).
// Object #128. Verb: multiply Prep: this none this Perms: +r +x
return (args[0] * args[1]);
// Object #256. Verb: square Prep: this none this Perms: +r +x
return #128:multiply(args[0], args[0]);
// Object #512. Verb: touch Prep: any any this Perms: +r
// Has a property (int) 'size' on it.
this.size = #256:square(this.size);
this.description = "It's a large button, it spans " + this.size + " metres.";
player:tell("You touch the button, it gets bigger.");
The user could then push button and the button object's size property would be squared.
Recommended Reading
I highly recommend you to read the document at http://bit.ly/17XIqjY for a more in-depth idea of how the system should work.
It is also recommended you read the following documents, as μMOO is based upon LambdaMOO and it’s methodology:
https://en.wikipedia.org/wiki/LambdaMOO
https://en.wikipedia.org/wiki/MOO
http://www.hayseed.net/MOO/manuals/ProgrammersManual_toc.html
http://www.moo.mud.org/
I take this question as asking for a language that could do what you need. That's what I'll try to answer.
First, this task is hopelessly unsuited to any mainstream or imperative language such as C# or Java. I wouldn't even think about it. Javascript is possible, but not what it's good at and nothing specific to recommend it.
Second, if you had the right skills, it would be an excellent opportunity to design an entirely new language and spend the next year or two getting it working. People really do that, but I don't recommend it unless you like that kind of masochistic experience. [I do.]
So my recommendation is that you widen your language experience until you find a match. Of the languages I know moderately well, Ruby is the best to try first. As soon as you said inject these variables into the verb you made me think of Ruby, because lots of Ruby software (including Rails) is built exactly like that. Forget Python, Perl and Javascript: I really don't think they will hack it.
Beyond Ruby you might contemplate Lua. I haven't used it much recently, and it may not suit, but it is widely used as a games scripting language.
Beyond that are the true functional languages. There is the most ancient of them all: Lisp. You can do absolutely anything in Lisp, including implementing the language you were looking for in the first place. Then there are Scala and Haskell, to name just two. They are mind-bending to learn, but well suited to the kind of problem you have.
Not much of an answer because it basically says: learn each of these languages in turn until you find one that works for you. [Happy to help further if I can. I have fond memories of Moo.]

Point of extension - Use case

Suppose I have a use case buy book, and the main flow is the following:
1- The user types the book code that he wants to buy
2- The system replies that there's enough stock of the requested book
3- The user confirm
Very simple.
Now suppose I want to give the option to the user to also do another thing between 2 and 3. How should I say it? I guess it's an extension to this use case, but I'm not sure where it's the point of extension.
As far as I know, if I choose, say, point of extension in 3, then the user has the opportunity to do 3 or do all the extension but not 3. The same behaviour of alternative flows.
But what I want is different. I want some "2.5" or nothing... do it or do nothing instead; not another thing.
I'm sorry for the vague question.
One option is the format recommended in Alistair Cockburn's Writing Effective Use Cases:
2a- User wants to do another thing:
2a1- The user does another thing
2a2- The system responds in some way, returns to step 3
Step 2a occurs after step 2 and before step 3. If the UC ends at step 2a2 then simply replace 'returns to step 3' with 'Use Case ends' or similar.
hth.
The problem here is the difference between the use case model in UML and use case descriptions. Extensions point is a concept from UML used to decouple extended and extending use cases. If you want adhere to this, you have to define the position of branching and returning back yourself, because UML says nothing about use case descriptions. I am personally as well as sfinnie a fan of Alistair Cockburns's approach to use cases, however it does not correspond well with the UML standard. There is yet another way, proposed by Bittner (Use case modeling book), who proposes to split the scenario into subflows with headlines.
I think what you actually want is an alternate path. An alternate path references a step in the main or in a different alternate path. I usually make that reference the Start step in the alternate. Then the End step is either a reference to where it returns to or an indication the path stops.

How to create a changelog?

I'm building a site that shows changes in deals that we have in our db. For example, if a deals status changes from pending to win, I want to show it, and if the value goes up or down, I want to show it, that kind of thing. Also, if you open the overview page, I want it to show the history of changes. So I need some kind of change logging, to be able to look in the past. How do I do this?
It is a rails project, but I think that's irrelevant.
I doubt there is any generic solution to this problem.
You can roll out your own. Start by considering all objects that need change logging. How many types are there? How often do you expect changes to occur? This will help you estimate the potential number of changes throughput you'll need to be dealing with. If there aren't too many, just stick them into database. If you are generating a lot, try storing to comma-separated-value file.
I have implemented a similar system before. I had 3 types of changes: 1) property value change, 2) adding of a value to a list, 3) removing value from a list.
I used the following format, stored in a log file:
//For type 1)
1,2011/01/01 00:00:00,MyObject,myProperty,oldValue,newValue
//For type 2)
2,2011/01/01 00:00:00,MyObject,myListProperty,addedValue
//For type 3)
3,2011/01/01 00:00:00,MyObject,myListProperty,removedValue
This captured most information I needed. The value parts were just some user-readable summary of the changed/added/removed property value.
Paper Trail Gem
Since you're on Rails, take a look at the PaperTrail gem. It does exactly what you're looking for and is beautifully built. You'll just need to add in a callback so that your overview page knows that a change occurred. But for the history of a model, just use the built-in PaperTrail functionality.

Does this Rails 3 Controller method make me look fat?

This is a new application, and I have an index method on a Search controller. This also serves as the home page for the application, and I'm trying to decide if I am headed down the wrong path from a design pattern perspective.
The method is already 35 lines long. Here is what the method does:
3 lines of setting variables to determine what "level" of hierarchical data is being searched.
Another 10 lines to populate some view variables based on whether a subdomain was in the request or not.
A 10 line section to redirect to one of two pages based on:
1) If the user does not have access, and is signed in, and has not yet requested access, tell them "click here to request access to this brand".
2) If the user does not have access, is signed in, and has already requested access, tell them "so and so is reviewing your request".
Another 10 lines to build the dynamic arel.
I can't get it straight in my head how to separate these concerns, or even if they should be separated. I appreciate any help you can offer!
Summarizing what you've said in something codelike (sorry, don't know ruby; consider it pseudocode):
void index() {
establishHierarchyLevel();
if (requestIncludedSubdomain())
fillSubdomainFields();
else
fillNonsubdomainFields();
if (user.isSignedIn() && !user.hasAccess()) {
if (user.hasRequestedAccess())
letUserIn();
else
adviseUserOfRequestUnderReview();
}
buildDynamicArelWhateverThatIs();
}
14 lines instead of 35 (of course, the bodies of the extracted methods will lengthen the overall code, but you can look at this and know what it's doing). Is it worth doing? That really depends on whether it's clearer to you or subsequent programmers. My guess is it's worth doing, that splitting out little code blocks into their own method will make the code easier to maintain.
That's a lot of variables being set. Maybe this is a good opportunity for a module of some kind? Perhaps your module can make a lot of these decisions for you, as well as acting as a wrapper for a lot of these variables. Sorry I don't have a more specific answer.
Without your code it's somewhat difficult to suggest actual fixes, but it definitely sounds like a really wrong approach and that you're making things much harder than they need to be:
3 lines of setting variables to
determine what "level" of hierarchical
data is being searched
if there is a search form, I would think you would want to pass those straight from the params hash into scopes or Model.where() calls. Setup scopes on your model as appropriate.
Another 10 lines to populate some view variables based on whether a subdomain was in the request or not.
This seems to me like it should be at most 1 line. or that in your view, you should use if statements to change what you'd like your output to be depending on your subdomain.
A 10 line section to redirect to one of two pages based on:
the only thing different in your explanation of the 2 views is "whether the user has requested access" surely this is just a boolean variable? You only need 1 view. Wrap the differences into 2 partials and then in your view and write one if statement to choose between them.
Another 10 lines to build the dynamic arel.
It might be necessary to go into Arel, but I highly highly doubt it. Your actual search call can in most cases (and should aim to be) 1 line, done through the standard ActiveRecord query interface. You want to setup strong scopes in your models that take care of joining to other models/narrowing conditions, etc. through the ActiveRecord Query interface.

Resources