How to override url_for_event in Apotomo to always include a parameter? - cucumber

To get to run Cucumber with my app and subdomains, I read that I should add default parameters to the default_url_options.
However, I can't seem to find a way to add default parameters to the url_for_event helper that Apotomo gives. I believe this would be the first (if not the only) step to getting integration tests, Apotomo, and subdomains to work.

I got an answer from Paul Hagstrom in the Apotomo mailing list:
class YourBaseWidget < Apotomo::Widget
def default_url_options
...
end
end
class YourOtherWidgets < YourBaseWidget
...
end
This works a lot like how most of your Rails controllers inherit from ApplicationController. Thus, anything you apply to ApplicationController will apply, by inheritance, to your child controllers.

Related

how to get hierarchy signals with similar name?

suppose in my test bench, i had following signals
top.module0.expect
top.module1.expect
yes, we instantiates module0/1 with same module
now, in a function get_expect_sig(int module_idx) (module_idx could be 0 or 1), i want to get the signal according to module_idx
what i'm currently do is as follow:
if (module_idx == 0) return top.module0.expect;
else if (module_idx == 1) return top.module1.expect;
this solution is pretty ugly.
if there any brilliant method that help me with this? because i got 32 modules, not 2 modules
thanks!
A case statement would be slightly less ugly. Since your design choose to instantiate the modules with unique names instead of an array of instances, there is no way to iterate over them.
Some tools allow you to look up a signal's value using a string name. Then you could format the string using the index value. You'll need to read your tools manual.
If this is testbench code (non-synthesizable), if module 0 and module 1 are instances of the same module, you could bind an interface to it. A bind statement will cause this interface to be instantiated in all instances of the module. You could then put some helper code in this interface to 'register' with a configuration class. In UVM, this could be the config db, but in SystemVerilog you could use a static or singleton class.
Example: https://www.edaplayground.com/x/5ZUG
This uses a few concepts (abstract-concrete polymorphic interfaces) that you may need to read about. But hopefully the example is clear enough.

Add renderer in #view_config from configuration?

How do I supply a configured value to a #view_config-decorated function or class?
E.g.
#view_config(route_name='example', renderer=some_config['template.name'])
class MyClass(BaseView):
...
Or
#view_defaults(route_name='example', renderer=some_config['template.name2'])
class MyClass2(BaseView):
...
Or
#view_config(route_name='example', renderer=some_config['template.name3'])
def method3(request):
...
It's very hard to know where to start, as I'm trying to edit a pyramid plugin, which pulls together its config in an includeme function, so it doesn't have anything obvious that I can include, and it's hard to know what's available to the #view_config decorator.
You can add views using declarative configuration (what you are doing now using #view_config or alternatively using imperative configuration by calling config.add_view() method.
In this case, as you need to access the Pyramid registry and settings file, it is easier to do adding the views imperatively.
In your __init__.py you can do:
settings = config.registry.settings
# You need to call config.add_route("foobar") to map view to URL also
config.add_view('views.MyClass', route_name="foobar", renderer=settings['template.name3'])
Then in your views.py:
class MyClass(BaseView):
pass
#view_config() and add_view() arguments are equal.
I thin kyou can also mix view_config and add_view() arguments for the same view, but I am not sure aobut this. Hope this helps.

cucumber: string to an active record model name

I'm trying to do a DRY cucumber feature and I'm facing a problem of converting a string into an ActiveRecord model name
Given /^the following "(.+)" exist:/ do |mod, table|
table.hashes.each do |t|
mod.create!(t)
end
assert mod.all.count == table.hashes.size
end
that gives
undefined method `create!' for "Balloon":String (NoMethodError)
More elegant solution might be to use a factory, but I'm wondering whether it is possible to use the above approach?
You could look into constantize which turns a String into a constant. Try:
"Balloon".constantize.create!(t)
BUT: Using your app code (models in particular) in a Cucumber step is code smell. Your integration tests shouldn't rely on the code under test at all—think of your app as a black box when you implement Cucumber steps. (Also think of a refactoring of your models that require you to go back and change your Cucumber steps—that's your first clue that you're on the wrong track!)
What you could do to improve this is create the models using an API (if your app implements one).
That way, you only rely on those parts of your app that are public-facing.
On another note: Your Given shouldn't have an assertion, it's more like a before hook in RSpec, setting up a condition for a later assertion...

What's a DRY solution for routes and controller with a polymorphic resource attached to both of a pair of nested resources?

Disclaimer: first month of developing with rails, but I have read everything I could find.
Edit: Somehow I missed this very similar question with a similar final answer.
I have polymorphic flags:
Class Flag...
belongs_to :flaggable, :polymorphic => true
...
end
I have nested resources that have the appropriate has_many :flags, :as => :flaggable statement.
resources :posts do
resources :comments
end
I would like both posts and comments and in the future other things on the site to be flaggable. What is the DRY/standard way (I'm using Rails 3.1) to do this with regard to routes and controller?
What I did for routes:
Mostly based on this rails cast, I made flags as a nested resource of both posts and comments. Already, I think I'm on the wrong track because it seems to be re-stating the polymorphic relationships in the models as well as breaking the guideline that "Resources should never be nested more than 1 level deep."
resources :posts do
resources :flags
resources :comments do
resources :flags
end
end
Alternatively, I thought to implement the flaggable routes separately as below. But again, this doesn't seem DRY and additionally makes non-desired independent routes for comments.
resources :posts do
resources :flags
end
resources :comments do
resources :flags
end
Finally, I wondered if I could make a generic resource for flaggables. I couldn't find any way to implement this and it has the same problem of the previous method of making general routes available for the generic flaggable type.
resources :flaggable do
resources :flags
end
What I did for the controller for the nested resources above:
I implemented find_flaggable, but realized that with nested resources, the parameter that gets converted to a flaggable class could be either Post or Comment since both end up in parameters (post_id and comment_id). I could solve the below with an id priority list for the current setup, but that is not a general solution and makes it even less DRY than it already is.
def find_flaggable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
So this is where I stopped (actually implemented a limited solution only for Posts and Comments) and realized I don't know a satisfying way to accomplish this. Can anyone help?
Just to solve the bit of DRYing up the code in your routes, a simple way would be to use
[:posts, :comments, :yet_another_resource].each do |resource_type|
resources resource_type do
resources :flags
end
end
instead of
resources :posts do
resources :flags
end
resources :comments do
resources :flags
end
This starts becoming really clean and useful when you have a lot of actions under your nested resources. Sort of like a simple version of the "Extract method" refactoring for your routes.
EDIT
In case you missed my comment, I think polymorphic_url is the right thing you are looking for!!
Since you're only trying to flag article instead of manipulate a flag resource collection I would just create a flag method in the Controllers on the resources you might want to flag.
From there you can just build the flag from the resource itself.
class PostsController < ApllicationController
...
def flag
#post = Post.find(params[:id])
flag = #post.flags.build(params[:flag])
if flag.save
flash[:notice] = "Post flagged"
else
flash[:notice] = "Unable to flag post"
end
render #post
end
end
routes.rb:
resources :posts do
post 'flag', :on => member
resources :comments do
post 'flag', :on => member
end
end
Something like that should work, it's not the DRYest way to do it. But it's probably how I would implement it.
Thanks for the answers. Here is what I ended up using, although I'm not totally satisfied with it. It seems like half-made polymorphism since the polymorphic resource's parent actually needs to be directly "typed" somewhere along the line. In jake's solution within the controller. In Azolo's by the routing of flags into various controllers. I had hoped to find a way to let Rails do the typing and simplify the application code.
To answer my first question about routes, it seems that no one is bothered about having unused routes caused by putting, for example, comments at a base level with flags as a child resource (as in Jake's answer). So I took Jake's answer there as the way to setup the routes. The polymorphic path helpers are nice too.
[:posts, :comments, :yet_another_resource].each do |resource_type|
resources resource_type do
resources :flags
end
end
That would then lead to using a flag controller that accepts the various polymorphic path helpers with something like find_flaggable from the railscast in my question.
def find_flaggable
params.each do |name, value|
if name =~ /(.+)_id$/
return $1.classify.constantize.find(value)
end
end
nil
end
Thanks again for the help.

SpecFlow -- Step (Given) with the same regex in different classes not executing independently

I have two classes (class A and B) both marked with [Binding]. Currently I'm using a class per feature. Classes A and B both have a step that looks like this:
[Given(#"an employee (.*) (.*) is a (.*) at (.*)")]
public void GivenAnEmployeeIsAAt(string firstName, string lastName, string role, string businessUnitName)
When I run the scenario for the features defined in class A, and the test runner executes the step indicated above, the matching step in class B gets executed instead.
Are "Steps" global as well? I thought only the "hook" methods are global, i.e. BeforeScenario, AfterScenario. I do not want this behavior for "Given", "Then", and "When". Is there any way to fix this? I tried putting the two classes in different namespaces and this didn't work either.
Also, am I potentially misusing SpecFlow by wanting each "Given" to be independent if I put them in separate classes?
Yes Steps are (per default) global. So you will run into trouble if you define two attributes that have RegExps that matches the same Step. Even if they are in separate classes.
Being in separate classes, or other placement (other assembly even) doesn't have anything to do with how SpecFlow groups it - it's just a big list of Given's, When's and Then's that it try to match the Step against.
But there's a feature called Scoped Steps that solves this problem for you. Check it out here: https://github.com/techtalk/SpecFlow/blob/master/Tests/FeatureTests/ScopedSteps/ScopedSteps.feature
The idea is that you put another attribute (StepScope) on your Step Defintion method and then it will honor that scoping. Like this for example:
[Given(#"I have a step definition that is scoped to tag (?:.*)")]
[StepScope(Tag = "mytag")]
public void GivenIHaveAStepDefinitionThatIsScopedWithMyTag()
{
stepTracker.StepExecuted("Given I have a step definition that is scoped to tag 'mytag'");
}
... or to scope an entire step definition class to a single feature:
[Binding]
[StepScope(Feature = "turning on the light should make it bright")]
public class TurningOnTheLightSteps
{
// ...
}
This step definition is using a StepScope for a tag. You can scope your steps by:
Tag
Scenario title
Feature title
Great question! I hadn't fully understood what that was for until now ;)

Resources