Rspec error: location has already been taken - ruby

I have following test case in rspec
let(:detail) { FactoryGirl.create(:detail) }
let(:url) do
"/detail/#{detail.detail_id}"\
"/user/#{detail.user.id}/details"
end
let(:location_header) do
detail_url(detail_id: detail.detail_id,
user: detail.user.id, location: detail.location)
end
before do
post url, params: params
end
context 'when valid location and value are passed, detail is created successfully.' do
let(:params) {{detail_app_id: detail.detail_app_id, location: detail.location, value: 'value'}}
it { expect(response.status).to eq(201) }
end
I am learning ruby rspec and i am getting an error "{\"error\":\"Validation failed: location has already been taken\"}"
when i change location value in params it passes but since i am creating factory girl for it i want to use that value rather passing different one.
Any idea why i am getting that error?

You need to go into one of your factories (I'm assuming detail) and assign its location properly (factories/detail.rb).
What's happening here is you're trying to create a new detail, which it attempts to save, but it fails the validation (I'm assuming you have a unique location validation on the detail model)
Check out http://www.rubydoc.info/gems/factory_bot/file/GETTING_STARTED.md, the sequences section, for how to give a unique attribute where required.

Related

Rails 5 API - Redundantly passing ID of Association

I have a simple structure of "A has_many B has_many C"
If I go into Rails console and do something like A.first.Bs.first.C.create() it'll create without an issue, however, if I use the API (or even Seeds actually) and so something like POST to /api/v1/a/1/b with the below create, I will always get rejected due to "Must belong to A" - Basically meaning it's trying to save as a.id = null.
A = Campaign. B = Party for the below snippet.
def create
#campaign = Campaign.find_by_id(params[:campaign_id])
if #campaign.user_id == current_user.id
#party = Party.new(party_params)
# #party.campaign_id = params[:campaign_id]
if #party.save!
render status: 201, json: {
message: "Successfully saved the party!",
party: #party,
user: current_user
}
else
render status: 404, json: {
message: "Something went wrong: Check line 27 of Party Controller"
}
end
end
end
The line I have commented out where I manually assigned #party.campaign_id resolved the error, but I am curious why it doesn't automatically pull from the information? Do route resources not function the same way as a Campaign.first.parties.create would?
Welcome any revision to this create method; It feels bulky, and likely not secure at all presently.
(Note #campaign.user_id == current_user.id is kind of a generic catch in case someone is trying to update someone else's campaign. I will likely re-visit this logic to make it more secure.)
Rails does not find anything automatically basing on routes, you need to do it by yourself.
In this case you can either assign id basing on params (as you did in the comment) or build Party as an element of Campaign.parties association
#campaign = Campaign.find_by_id(params[:campaign_id])
#party = #campaign.parties.new(party_params)

How to make an optional strong parameters key but filter nested params?

I have this in my controller:
params.require(:item).permit!
Let's assume this rspec spec, which works as expected:
put :update, id: #item.id, item: { name: "new name" }
However, the following causes ActionController::ParameterMissing:
put :update, id: #item.id, item: nil
It has to do with controller macros that I use for other actions and through which I cannot control the params being sent (the macros checks for user credentials, so I don't really care about actually testing an #update action, rather I just test before_filters for it).
So my question is: How do I make params[:item] optional, yet still filter attributes within it if it's present?
What about:
params.require(:item).permit! if params[:item]
You cannot require an optional parameter. That is contradictory.
Edit: as mtjhax mentioned in his comment, there is advice from here to use fetch instead: params.fetch(:item, {}).permit!

Error while executing perl application in locate.pl

This is the error:
No 'new' for class 'Spec::Benchmark::bzip2401' in 'C:/Users/Tester/Documents/SpecINT2k6_WoT/benchspec/CPU2006/401.bzip2/Spec/object.pm'
point of error in locate.pl file:
my $class="Spec::Benchmark::${name}${num}";
if (!$class->can('new')) {
Log(0, "\nNo 'new' for class '$class' in '$pm'\n");
next;
}
here is the link to the whole locate.pl file http://ks.tier2.hep.manchester.ac.uk/Repositories/other-software/SPEC_CPU2006v1.1/bin/locate.pl
This is the object.pm file http://codepad.org/O196ykIq
I am getting this error while running Specint2006 suite, but this error is not related to the suite. Can anyone tell me what does !$class->can('new') do and why is it returning true here?
Thanks.
Can checks if the Class has the method. The return value is always the coderef. If the class dont know the method, the return value is undef.
The Class dont know the new method, so its false. But you call it with not
!$class->can('new')
Quote from HERE
Again, the same rule about having a valid invocand applies -- use an eval block or blessed if you need to be extra paranoid.

How to print validation error outside of field constructor in Play framework 2

How can I show a validation error for a form field outside of a field constructor in Play framework 2? Here is what I tried:
#eventForm.("name").error.message
And I get this error:
value message is not a member of Option[play.api.data.FormError]
I'm confused because in the api docs it says message is a member of FormError. Also this works fine for global errors:
#eventForm.globalError.message
You can get a better grasp of it checking Form's sourcecode here
Form defines an apply method:
def apply(key: String): Field = Field(
this,
key,
constraints.get(key).getOrElse(Nil),
formats.get(key),
errors.collect { case e if e.key == key => e },
data.get(key))
That, as said in the doc, returns any field, even if it doesn't exist. And a Field has an errors member which returns a Seq[FormError]:
So, you could do something like that (for the Seq[FormError]):
eventForm("name").errors.foreach { error =>
<div>#error.message</div>
}
Or (for the Option[FormError])
eventForm("name").error.map { error =>
<div>#error.message</div>
}
Or, you could use Form errors:
def errors(key: String): Seq[FormError] = errors.filter(_.key == key)
And get all errors of a given key. Like this (for the Seq[FormError]):
eventForm.errors("name").foreach { error =>
<div>#error.message</div>
}
Or (for the Option[FormError])
eventForm.error("name").map { error =>
<div>#error.message</div>
}
If you want more details, check the source code. It's well written and well commented.
Cheers!
EDIT:
As biesior commented: to show human readable pretty messages with different languages you have to check how play works I18N out here
To be thorough you're probably going to have to deal with I18N. It's not hard at all to get it all working.
After reading the documentation you may still find yourself a bit consufed. I'll give you a little push. Add a messages file to your conf folder and you can copy its content from here. That way you'll have more control over the default messages. Now, in your view, you should be able to do something like that:
eventForm.errors("name").foreach { error =>
<div>#Messages(error.message, error.args: _*)</div>
}
For instance, if error.message were error.invalid it would show the message previously defined in the conf/messages file Invalid value. args define some arguments that your error message may handle. For instance, if you were handling an error.min, an arg could be the minimum value required. In your message you just have to follow the {n} pattern, where n is the order of your argument.
Of course, you're able to define your own messages like that:
error.futureBirthday=Are you sure you're born in the future? Oowww hay, we got ourselves a time traveler!
And in your controller you could check your form like that (just one line of code to show you the feeling of it)
"year" -> number.verifying("error.furtureBirthday", number <= 2012) // 2012 being the current year
If you want to play around with languages, just follow the documentation.
Cheers, again!
As you said yourself, message is a member of FormError, but you have an Option[FormError]. You could use
eventForm("name").error.map(_.message).getOrElse("")
That gives you the message, if there is an error, and "" if there isn't.

How to create XML object from string using xml-mapping in Ruby

I'm using xml-mapping in Ruby (on Sinatra) for some XML stuff. Generally I follow this tutorial: http://xml-mapping.rubyforge.org/. I can create objects and write them to XML strings using
login.save_to_xml.to_s
But when I try
login = Login.load_from_xml(xml_string)
I get the following error:
XML::MappingError - no value, and no default value: Attribute username not set (XXPathError: path not found: username):
Here is the XML string I receive:
<login><username>ali</username><password>baba</password></login>
This is what the class looks like:
class Login
include XML::Mapping
text_node :username, "username"
text_node :password, "password"
end
So the class name is the same, the nodes are named the same. I actually get the exact same string when I create an instance of my object and fill it with ali/baba:
test = Login.new
test.username = "ali"
test.password = "baba"
p test.save_to_xml.to_s
<login><username>ali</username><password>baba</password></login>
What am I missing?
Thanks,
MrB
EDIT:
When I do
test = login.save_to_xml
And then
login = Login.load_from_xml(test)
it works. So the problem seems to be that I'm passing a string, while the method is expecting.. well, something else. There is definitely a load_from_xml(string) method in the rubydocs, so not sure what to pass here. I guess I need some kind of reverse to_s?
It looks like you save_to_xml creates a REXML::Element. Since that works, you may want to try:
Login.load_from_xml(REXML::Document.new(xml_string).root)
See the section on "choice_node" for a more detailed example http://xml-mapping.rubyforge.org/

Resources